Shared Source C# Compiler

The Microsoft® Shared Source implementation of the C# compiler is very similar to the implementation in Microsoft® .NET Framework SDK and Microsoft® Visual Studio® .NET.  Both implementations originate from the same source base but some options and features that rely on functionality in Microsoft Windows have been removed from the Shared Source implementation.

Changes in the Shared Source C# Implementation

The following option areas and related options were modified

Complete documentation for the .NET Framework C# compiler and tutorials are available on msdn.microsoft.com.   Other than the changes mentioned above, the functionality in the Shared Source implementation of the C# compiler is the same as the .NET Framework implementation.

Note: Because the C# compiler builds complete portable executable (PE) files (.exes, .dlls, netmodules) it can be considered a compiler and linker.  The compiler has linker-style options that set Windows-specific information in the generated PE file.  The Shared Source implementation of the CLI loads applications using the clix loader and does not use the Window system loader at all.  This means that Windows-specific PE settings are ignored even when Shared Source is running on Windows.  However, if the generated assembly is executed under the .NET Framework the file will be loaded by the system loader and the information in the PE file will be used.  Some of the options in this category are:

The C# Command Line

The compiler produces PE format executable (.exe) files, dynamic-link libraries (.dll), or code modules (.netmodule).  For information on the PE format of files loaded by ECMA CLI implementations see the ECMA-335 Common Language Infrastructure specification, Partition II, section 24.

For information on how the Shared Source CLI (SSCLI) implementation loads PE files, see the implemention code for the clix application loader: sscli\clr\src\tools\clix.  Also see sscli\clr\src\vm\cormap.cpp for the PE loader implementation.

The C# compiler can be invoked at the command line by typing the name of its executable file (csc.exe on Windows and just csc on FreeBSD) on the command line.   See env.html for more details on correctly setting your environment to build and execute SSCLI utilities including the C# compiler.

Rules for Command-Line Syntax

The C# compiler code uses the following rules when interpreting arguments given on the operating system command line:

C# Compiler Options

Every compiler option is available in two forms: -option and /option.

[FreeBSD note: To avoid  ambiguity between filenames and '/' on FreeBSD and the /option form, use the -option form.]

Option Purpose
@response_file
Specify a response file, response_file.

The @ option lets you specify a file that contains compiler options and source code files to compile. These compiler options and source code files will be processed by the compiler just as if they had been specified on the command line.

To specify more than one response file in a compilation, specify multiple response file options. For example:


@file1.rsp @file2.rsp


In a response file, multiple compiler options and source code files can appear on one line. A single compiler option specification must appear on one line (cannot span multiple lines). Response files can have comments that begin with the # symbol.

Specifying compiler options from within a response file is just like issuing those commands on the command line.

The compiler processes the command options as they are encountered. Therefore, command line arguments can override previously listed options in response files. Conversely, options in a response file will override options listed previously on the command line or in other response files.

/?
Output compiler options to the console.  Same as /help

This option sends a listing of compiler options, and a brief description of each option, to stdout. If this option is included in a compilation, no output file will be created and no compilation will take place.

/addmodule:file[;file2]
Specify one or more modules (file, file2, and so on) to be part of this assembly.

All modules added with /addmodule must be in the same directory as the output file at run time. That is, you can specify a module in any directory at compile time but the module must be in the application directory at run time. If the module is not in the application directory at run time, you will get a System.TypeLoadException.

file cannot contain an assembly. For example, if the output file was created with /target:module, its metadata can be imported with /addmodule.

If the output file was created with a /target option other than /target:module, its metadata cannot be imported with /addmodule but can be imported with /reference.

/baseaddress:address_value
Specify the preferred base address (address_value) at which to load a DLL. 

The loading base address is only relevant if the file is going to be run on the Microsoft .NET Framework on Windows.  Differences in loading behavior can occur in the .NET Framework on Windows depending on the version of the Windows operating system.

The SSCLI implementation ignores base address information stored in the PE assembly file.  For details on how the SSCLI implementation loads PE files see sscli\clr\src\vm\cormap.cpp.

For information on the PE format of files loaded by ECMA CLI implementations see the ECMA-335 Common Language Infrastructure specification, Partition II, section 24.

For more information on DLL base addresses in Windows, go to msdn.microsoft.com and search for "dll base address".  The Microsoft Windows Platform SDK tool Rebase.exe documentation and the Microsoft Visual Studio Linker.exe documentation provide detailed information.

/checked[+ | -]
Specify whether integer arithmetic that overflows the bounds of the data type will cause an exception at run time.

The /checked option specifies whether an integer arithmetic statement that is not in the scope of the checked or unchecked keywords and that results in a value outside the range of the data type shall cause a run-time exception.

An integer arithmetic statement that is in the scope of the checked or unchecked keywords is not subject to the effect of the /checked option.

If an integer arithmetic statement that is not in the scope of the checked or unchecked keywords results in a value outside the range of the data type, and /checked+ (default if option not specified) is used in the compilation, then that statement will cause an exception at run time. If /checked- is used in the compilation, then that statement will not cause an exception at run time.

/codepage:id
Specify the code page to use for all source code files in the compilation with id.

If you compile one or more source code files that were not created to use the default code page on your computer, you can use the -codepage option to specify which code page should be used.

If the source code files were created with the same codepage that is in effect on your computer or if the source code files were created with UNICODE or UTF-8, you need not use -codepage.

/debug[+ | -]
/debug:{full | pdbonly}
Emit debugging information.

The /debug option causes the compiler to generate debugging information and place it in the output file(s). Use this option to create debug builds. If /debug, /debug+, or /debug:full is not specified, you will not be able to debug the output assembly at the source code level.

Options values:

+ | -

Specifying +, or just /debug, causes the compiler to generate debugging information and place it in a program database (.ildb file). Specifying -, which is in effect if you do not specify /debug, causes no debug information to be created.

full | pdbonly

Specifies the type of debugging information generated by the compiler. The full argument, which is in effect if you do not specify /debug:pdbonly, enables attaching a debugger to the running program. Specifying pdbonly allows source code debugging when the program is started in the debugger but will display CIL only when the running program is attached to the debugger.

/define:name[;name2]
Define preprocessor symbols where name, name2 are the names of one or more symbols that you want to define.
 

The /define option defines name as a symbol in your program. It has the same effect as using a #define preprocessor directive in your source file. A symbol remains defined until an #undef directive in the source file removes the definition or the compiler reaches the end of the file.

You can use symbols created by this option with #if, #else, #elif, and #endif to compile source files conditionally.

/d is the short form of /define.

You can define multiple symbols with /define by using a semicolon or comma to separate symbol names. For example:

/define:DEBUG;TUESDAY

/filealign:number
Specify the size of sections in the output file where number is a value that specifies the size of sections in the output file. Valid values are 512, 1024, 2048, 4096, 8192, and 16384. These values are in bytes.

The information stored in the PE file as the result of using this option is not used on the SSCLI implementation of the CLI assembly loader.  It would only be meaningful if the file is going to be loaded in the Microsoft .NET Framework on Windows.

/fullpaths
Specify the absolute path to the file in compiler output.

By default, errors and warnings that result from compilation specify the name of the file in which an error was found. The /fullpaths option causes the compiler to specify the full path to the file.

/help
Output compiler options to the console.  Same as /?

This option sends a list of compiler options, and a brief description of each option, to stdout. If this option is included in a compilation, no output file will be created and no compilation will take place.

/incremental[+ | -]
Enable incremental compilation of source code files.

The /incremental compiler option enables the incremental compiler, which compiles only those methods that have changed since the last compilation. Information about the state of the previous compilation is stored in the following files, which are created by the compiler:

output_file_name.dbg

When compiling with /debug, the status of debug information is stored in the program database (.pdb file).

output_file_name.extension.incr

Information about the status of the compilation, other than debug information, is stored in a .incr file.
The first time you use /incremental, the .incr and .pdb files are updated and all subsequent compilations will be incremental. Changing the compiler options from the previous /incremental compilation will cause a full rebuild of the .incr and .ildb files.

If the compiler detects many changes since the last incremental build, it may perform a full build, as a full build would be at least as efficient as an incremental build. If the compiler cannot find the project's .ildb or .incr files, a full rebuild will occur.

By default, /incremental- is in effect. Specifying /incremental is the same as specifying /incremental+.

Output files created with the /incremental option may be larger than those created with incremental compilation disabled. Because the output files can be larger, you should use /incremental- in the final build of your output file.

/incremental will speed compilation on projects with many, smaller files, and will have little or no speed improvement on projects with fewer, large files.

/lib:dir1[, dir2]
Specify the location of assemblies referenced via /reference where dir1 is a directory for the compiler to look in if a referenced assembly is not found in the current working directory (the directory from which you are invoking the compiler) or in the CLI implementation assembly directory and dir2 is one or more additional directories to search in for assembly references.

Separate additional directory names with a comma.

The /lib option specifies the location of assemblies referenced via the /reference option.

The compiler searches for assembly references that are not fully qualified in the following order:

  1. The current working directory. This is the directory from which the compiler is invoked.
  2. The common language runtime system directory.
  3. Directories specified by /lib.
  4. Directories specified by the LIB environment variable.

Use /reference to specify an assembly reference.

/lib is additive; specifying it more than once appends to any prior values.

An alternative to using /lib is to copy into the working directory any required assemblies; this will allow you to simply pass the assembly name to /reference. Following compilation you can then delete the assemblies from the working directory. Since the assembly references store only the assembly name and not do not specify a path, the application can be started on the target computer and will find and use the assembly in the global assembly cache.

/linkresource:filename[,identifier]
Create a link to a managed resource where filename is the resource file to which you want to link from the assembly and identifier (optional) is the logical name for the resource (the name used in code to load the resource).

The default identifier is the name of the file name.

The /linkresource option creates a link to a resource in the output file; the resource file is not placed in the output file. The /resource option embeds a resource file in the output file.

Linked resources are public in the assembly when created with the C# compiler.

/linkresource requires one of the /target options other than /target:module.

If filename is a .resource file created by resgen.exe or in the development environment, for example, it can be accessed with members in the System.Resources namespace. For all other resources, use the GetManifestResource* methods in the System.Reflection.Assembly class to access the resource at run time.

/linkres is the short form of /linkresource.

/main:class
Specify the location of the Main method where class is the type that contains the Main method.

If your compilation includes more than one type with a Main method, the /main option allows you to specify which type contains the Main method that you want to use as the entry point into the program.

This option is only for use when compiling an .exe file.
/nologo
Suppress compiler banner information.

The /nologo option suppresses the display of the sign-on banner when the compiler starts up and display of informational messages during compiling.

/nostdlib[+ | -]
Do not import the base class library (mscorlib.dll).

The /nostdlib option prevents the import of mscorlib.dll, which defines the entire System namespace. Use this option if you want to define or create your own System namespace and objects.

This option is used by the SSCLI build system in order to bootstrap the build of the base class libraries in mscorlib.dll.

If you do not specify /nostdlib, mscorlib.dll will be imported into your program (same as specifying /nostdlib-). If you specify /nostdlib, it is the same as specifying /nostdlib+.

/noconfig
Do not compile with the global or local versions of csc.rsp.

The /noconfig option tells the compiler not to compile with the global or local versions of csc.rsp.

By default, the C# compiler looks in the compiler directory and then in the directory from which the compiler was invoked for files called csc.rsp.

The csc.rsp file that is supplied in compiler directory references the .NET Framework assemblies that would be included in a C# project created in the Visual Studio development environment. You can modify this global csc.rsp file to contain any other compiler options that you want to include in a compilation from the command line with csc.exe.

The compiler will then look for a csc.rsp file in the directory from which the compiler was invoked. The commands in the local csc.rsp file will be combined with the global csc.rsp file for the compilation. Since the global csc.rsp is processed first, any command that is also in the local csc.rsp file will override the setting of the same command in the global csc.rsp file.

Finally, the compiler will read the options passed to the csc command. Since these are processed last, any option on the command line will override a setting of the same option in either of the csc.rsp files.

If you do not want the compiler to look for and use the settings in the global and local csc.rsp files, specify /noconfig.

/nowarn:number1[,number2[...]]
Suppress the compiler's ability to generate specified warnings where number1, number2 are warning numbers that you want the compiler to suppress.

The /nowarn option lets you suppress the compiler's ability to generate one or more warnings. Separate multiple warning numbers with a comma.

You only need to specify the numeric part of the warning identifier. For example, if you want to suppress CS0028, you could specify /nowarn:28.

/optimize[+ | -]
Enable/disable optimizations.

The /optimize option enables or disables optimizations performed by the compiler to make your output file smaller, faster, and more efficient. /optimize also tells the common language runtime to optimize code at runtime.

By default, /optimize is in effect. Specifying /optimize is the same as specifying /optimize+.

All references (see /reference) in an assembly must have the same optimization settings.

To prevent the compiler from generating an optimized output file, specify /optimize-.

/o is the short form of /optimize.

It is possible to combine the /optimize and /debug options.

/out:filename
Specify output file where filename is the name of the output file created by the compiler.

The /out option specifies the name of the output file. The compiler expects to find one or more source code files following the /out option.

If you do not specify the name of the output file:

  • An .exe will take its name from the source code file that contains the Main method.
  • A .dll or .netmodule will take its name from the first source code file.

On the command line, it is possible to specify multiple output files for your compilation. All source code files specified after an /out option will be compiled into the output file specified by that /out option. A source code file used to compile one output file cannot be used in the same compilation for the compilation of another output file.

Specify the full name and extension of the file you want to create.

/recurse:[dir\]file
Search subdirectories for source files to compile where dir (optional) is the directory in which you want the search to begin and file is the file(s) to search for. Wildcard characters are allowed.

If dir not specified, the search begins in the directory where the csc command was executed.

The /recurse option lets you compile source code files in all child directories of either the specified directory (dir) or the directory where the csc command was executed.

You can use wildcards in a file name to compile all matching files in the project directory without using /recurse.

/reference:file[;file2]

 
Imports the information in the metadata of the referenced assembly for use in resolving external references where file, file2 are one or more files that contains an assembly manifest. To import more than one file, separate file names with either a comma or a semicolon.

The /reference option causes the compiler to make public type information in the specified files available to the project you are currently compiling.

The file(s) you import must contain a manifest; the output file must have been compiled with one of the /target options other than /target:module.

Use /addmodule to import metadata from an output file that does not contain an assembly manifest.

Although executable files can be referenced as assemblies it is not recommended that shared, referenced code be stored in executable assemblies.  Instead, place the shared code in an assembly library.

If you reference an assembly (Assembly A), which itself references another assembly (Assembly B), you will need to reference Assembly B if:

  • A type you use from Assembly A inherits from a type or implements an interface from Assembly B.
  • If you invoke a field, property, event, or method that has a return type or parameter type from Assembly B.

Use /lib to specify the directory in which one or more of your assembly references is located. The /lib topic also discusses which directories the compiler searches for assemblies.

/r is the short form of /reference.

/resource:filename[,identifier]
Embed a .NET Framework resource into the output file where filename is the SSCLI resource file you want to embed in the output file and identifier (optional) is the logical name for the resource; the name used in code to load the resource.

The default identifier is the name of the file name.

Use /linkresource to link a resource to an assembly and not place the resource file in the output file.

Resources are public in the assembly when created with the C# compiler.

If filename is a SSCLI resource file created, for example, by resgen.exe or in the development environment, it can be accessed with members in the System.Resources namespace (see System.Resources.ResourceManager for more information). For all other resources, use the GetManifestResource* methods in the System.Reflection.Assembly class to access the resource at run time.

/res is the short form of /resource.

/target:output_type
Specifies the type of assembly to be generated by the compiler.

Unless you specify /target:module, /target causes assembly metadata to be placed in an output file.

The assembly manifest is placed in the first .exe output file in the compilation or in the first .dll, if there is no .exe output file. For example, in the following command line, the manifest will be placed in 1.exe:

csc /out:1.exe t1.cs /out:2.netmodule t2.cs

The compiler creates only one assembly manifest per compilation. Information about all files in a compilation is placed in the assembly metadata. All output files except those created with /target:module can contain an assembly manifest. When producing multiple output files at the command line, only one assembly manifest can be created and it must go into the first output file specified on the command line. If the first output file is an executable assembly (/target:exe or /target:winexe) or code library (/target:library), then any other output files produced in the same compilation must be modules (/target:module).

If you create an assembly, you can indicate that all or part of your code is CLS compliant with the CLSCompliant attribute.

Specify the format of the output file using one of four options:

/target:exe
The /target:exe option causes the compiler to create an executable (EXE), console application. The /target:exe option is in effect by default. The executable file will be created with the .exe extension.

Unless otherwise specified with the /out option, the output file name takes the name of the input file that contains the Main method.

When specified at the command line, all files up to the next /out, /target:winexe, or /target:library option are used to create the .exe file. The /target:exe option is in effect for all files since the previous /out or /target:library option.

One and only one Main method is required in the source code files that are compiled into an .exe file. The /main compiler option lets you specify which class contains the Main method, in cases where your code has more than one class with a Main method.
/target:library
The /target:library option causes the compiler to create a dynamic-link library (DLL) rather than an executable file (EXE). The DLL will be created with the .DLL extension.

Unless otherwise specified with the /out option, the output file name takes the name of the first input file.

When specified at the command line, all files up to the next /out, /target:winexe, or /target:exe option are used to create the .dll file.

When building a .DLL file, a Main method is not required.
/target:module
To not generate an assembly manifest, use the /target:module option. By default, the output file will have an extension of .netmodule.

A file that does not have an assembly manifest cannot be directly loaded by the SSCLI execution engine. However, such a file can be incorporated into the assembly manifest of an assembly via /addmodule.

If more than one module is created in a single compilation, internal types in one module will be available to other modules in the compilation. When code in one module references internal types in another module, then both modules must be incorporated into an assembly manifest, via /addmodule.
/target:winexe
The /target:winexe option causes the compiler to create an executable (EXE) Windows GUI application. The executable file will be created with the .exe extension. This sets the subsystem information in the PE file which is used by Microsoft Windows to determine some execution characteristics of the application.

This information is ignored by the SSCLI implementation of the CLI.  However, if the application is executed under the Microsoft .NET Framework this option will have an impact.  For more information on the console or Windows subsystem setting see http:msdn.microsoft.com and search for "console subsystem".

Use /target:exe to create a console application.

Unless otherwise specified with the /out option, the output file name takes the name of the input file that contains the Main method.

When specified at the command line, all files until the next /out or /target option are used to create the Windows application.

Only one Main method is required in the source code files that are compiled into an .exe file. The /main option lets you specify which class contains the Main method, in the case where your code has more than one class with a static Main method.

/unsafe
The /unsafe option allows code that uses the unsafe keyword to compile.
/utf8output
Display compiler output using UTF-8 encoding.

The /utf8output option displays compiler output using UTF-8 encoding.

In some international configurations, compiler output cannot correctly be displayed in the console. In these configurations, use /utf8output and redirect compiler output to a file.

/warn:option
The /warn option specifies the warning level for the compiler to display.

The Build Errors documentation describes the warnings, indicates each warning's level, and indicates potential problems (rather than actual coding errors) with statements that may not compile as you intend.

Use /warnaserror to treat all warnings as errors.

/w is the short form of /warn.

option is the minimum warning level you want displayed for the build.

Valid values are 0-4:

Warning Level Meaning
0 Turns off emission of all warning messages.
1 Displays severe warning messages.
2 Displays level 1 warnings plus certain, less-severe warnings, such as warnings about hiding class members.
3 Displays level 2 warnings plus certain, less-severe warnings, such as warnings about expressions that always evaluate to true or false.
4 Displays all level 3 warnings plus informational warnings. This is the default warning level at the command line.
/warnaserror[+ | -]
Promote warnings to errors.

The /warnaserror+ option treats all warnings as errors. Any messages that would ordinarily be reported as warnings are instead reported as errors, and the build process is halted (no output files are built).

By default, /warnaserror- is in effect, which causes warnings to not prevent the generation of an output file. /warnaserror, which is the same as /warnaserror+, causes warnings to be treated as errors.

Use /warn to specify the level of warnings that you want the compiler to display.

Sample Command Lines

Copyright (c) 2002 Microsoft Corporation