Compilers and Builds

.Compilers and Builds



Command Line Compiler | Conditional Compilation with Preprocessor Directives

C# applications can be compiled and built with the Visual Studio IDE, with MSBuild from the command line, or with an automated process using Team Foundation Build. Different configurations and project properties can be used to specify different kind of builds, such as for debugging/production or for different hardware platforms. Inline directives can also be inserted in the code for conditional compilation. C# also has a command line compiler (csc.exe) that produces executable (.exe) files, dynamic-link libraries (.dll), or code modules (.netmodule).

Command Line Compiler

The C# command line compiler (csc.exe) compiles C# code into Common Intermediate Language (CIL) stored in an assembly (.dll, .exe). When the assembly is executed, the CLR reads the assembly and compiles its contents into native code that will run on the hardware.

To get help with the command line compiler enter =>

C:\Windows\Microsoft.NET\Framework\v4.0.30319>csc.exe /?

Example of creating a console application with simple tools (Notepad and csc.exe):

  1. Open Notepad (notepad myprogram.cs) and enter the following code:
    using System;
    
    public class myProgram
    {
     public static void Main()
     {
      if (DateTime.Now.DayOfWeek == DayOfWeek.Friday)
       Console.WriteLine("It's Friday!");
      else
       Console.WriteLine("Sorry, it's not Friday yet.");
     }
    }


  2. To compile the program enter =>

    C:\Windows\Microsoft.NET\Framework\v4.0.30319>csc /out:c:\users\kevin\myprogram.exe c:\users\kevin\myprogram.cs


Sample Command Line Compiles

Examples of using the command line compiler:

  • csc File.cs - produces .exe file
  • csc /target:library File.cs - produces .dll file
  • csc /out:My.exe File.cs - compiles File.cs and creates My.exe
  • csc /define:DEBUG /optimize /out:File2.exe *.cs - compiles all the C# files in the current directory, with optimizations on and defines the DEBUG symbol. The output is File2.exe.
  • csc /target:library /out:Something.xyz *.cs - compiles all the C# files in the current directory to Something.xyz (a DLL):


MSBuild

MSBuild provides an XML schema for a project file that controls how the build platform processes and builds software. Visual Studio uses MSBuild, but it doesn't depend on Visual Studio. By invoking msbuild.exe on your project or solution file, you can orchestrate and build products in environments where Visual Studio isn't installed.

MSBuild.exe [Switches] [ProjectFile]


Useful Commands
  • dir /s %WINDIR%\Microsoft.NET\CSC.EXE - find different version of C# compiler on computer.
  • msbuild.exe /? - MSBuild help
  • MSBuild.exe MyProj.proj /property:Configuration=Debug - build the file MyProj.proj with the Configuration property set to Debug.


Top




Conditional Compilation with Preprocessor Directives

The C# compiler does not have a separate preprocessor, but the directives are processed as if there were one. They are used to help in conditional compilation. A C# preprocessor directive must be the only instruction on a line. Unlike C and C++ directives, you cannot use C# directives to create macros. C# preprocessor directives include:

  1. #if
  2. #else
  3. #elif
  4. #endif
  5. #define
  6. #undef
  7. #warning
  8. #error
  9. #line
  10. #region
  11. #endregion
  12. #pragma
  13. #pragma warning
  14. #pragma checksum

Note: C# Compiler errors and warnings can be found at C# Compiler Errors and Warnings.

Preprocessor Directives

#define KEVINTEST_1
#define KEVINTEST_2

#pragma warning disable 414, 3021

class Program
{
    static void Main()
    {
#if (KEVINTEST_1)
    System.Console.WriteLine("KEVINTEST_1 is defined.");
    #warning KEVINTEST_2 is defined
    #if(KEVINTEST_2)
        System.Console.WriteLine(" and KEVINTEST_2 is defined.");
        #error KEVINTEST_2 is defined
    #endif
#elif (KEVINTEST_2)
    System.Console.WriteLine("KEVINTEST_2 is defined.");
#else
    System.Console.WriteLine("NO KEVINS defined.");
#endif

#pragma warning restore 3021
    }
}

Top



Reference Articles

Top