Use CSC.exe to compile Visual C # code files to explain the specific role of CSC parameters and switches

Source: Internet
Author: User
Tags define command line final functions integer mscorlib switches win32
visual| compilation

The compiler for Visual C # is significantly different from the compilers of previous programming languages. The biggest difference is that the previous program compiler is to compile the program code to build the machine language that can be used directly for the computer. Although the Visual C # compiler can also compile the written program code into an EXE or a DLL file, this file is just an Il file (intermediate language), which cannot be used directly by the computer. It is only when this IL file is invoked that a JIT (just-in-time) compiler is used to generate this IL file into machine code that is available to the computer.

Visual C # 's compilation process can be roughly divided into two parts, the first part, from the program code to the Il file, the process is done through human intervention, that is, through CSC.exe, the second part, from IL file to machine language, this project is automatically implemented by the machine. This article will specifically explain the first section, how to use CSC.exe to properly compile the Visual C # files.

CSC.exe has many parameters and switch options when compiling Visual C # program code into IL files. The correct understanding and application of these parameters and switches can sometimes solve seemingly intractable problems. Here is a table to outline the specific functions of these parameters and switches. These parameters and switch options are sorted alphabetically. Which with "*", is a number of commonly used parameters or switches.

Option Purpose
@ * Specifies the response file.
/?,/help Displays the compiler's options on the console's computer screen
/addmodule specifies one or more modules as part of the Assembly
/baseaddress Specifies the base address of the loaded DLL
/bugreport creates a file that contains information that is easier to report errors
/checked If the integer evaluates the bounds of the overflow data type, an exception event is generated at run time
/codepage all source code files that specify the code page for use in compilation
/debug * Send debugging information
/define defines preprocessor symbols for
/doc * Annotate the processed document as an XML file
/FULLPATHS specifies the path to compile the output file anyway
/incremental the source code to the file for incremental compilation
/linkresource link. NET resources to the collection
/main Specify the location of the Main method
/nologo prohibit use of compiler flag information
/nooutput compile files without exporting files
/nostdlib does not export a standard library (that is, mscorlib.dll)
/nowarn compile but the compiler does not display a warning function
/optimize turn optimizations on or off
/out * Specify Output file
/recurse Search for subdirectories of compiled source files
/reference * Import meta data from a file containing a collection
/target * Specify the format of the output file
/unsafe compile code that uses unsafe keywords
/warn Set Warning Level
/warnaserror Elevation warning as error
/win32icon inserts an. ico file to the output file
/win32res Inserts a WIN32 resource guide output file
Specific Description:

A @

This option is used to specify the response file. A response file is a file that contains a number of compilation options. These compilation options are handled by the compiler along with the source code files. Generally speaking, this kind of response file appears as a text file. His extension is. rsp. A comment that starts with a # symbol in the response file.

For example: The following is the content of a response file RESP1.RSP:

# This is a simple response file, the filename is called RESP1.RSP

#使用方法: csc @resp1. RSP

/target:exe/out:sample.exe Sample.cs

The function of this response file is to compile the Sample.cs file into a Sample.exe file. If you want to specify multiple response files in a compilation, you can specify multiple response file options, such as:

@file1. RSP @file2. RSP

Two /? and/help

This option should be needless to say, those who have used DOS programs will probably use this option.

Three /addmodule

This option is to enable the compiler to  of the type from the project that the user is compiling to the available files. All modules that have been added to the/addmodule must be in the same directory as the output file at run time. This means that the user can specify a module in any directory at compile time, but the module must be in the application directory at run time. The list of compilations cannot be included in the document. For example, if the output file is created with/taarget:module, its metadata can be imported with/addmodule.

Example: Add two modules to the MyProject.cs

Csc/addmodule:module1.dll;module2.dll MyProject.cs

Four /baseaddress

This option allows the user to specify the preferred address for loading the DLL, which can be decimal, 16, octal. The default preferred address for DLLs is set at the. NET Runtime. This option is ignored if the destination file is not a DLL file.

Example: MyLibrary.cs the compiler DLL file, and when the DLL is loaded in the. NET runtime environment, the address is 0x1111000

Csc/baseaddres:0x1111000/target:library MyLibrary.cs
Five /bugreport

This option is used to report compile-time error messages. Include the following in the report:

1. A copy of all source code in the compilation

2. All compilation options in the compilation

3. Compile information, including compiler, run time, operating system version information

4). Compiler output

5). Description of the problem

6. How to solve the problem description

Example: Generate a Bugs.txt file, and put the error report inside the file

Csc/bugreport:bugs.txt Hello.cs

Six /checked

This option specifies whether an integer evaluation statement that does not validate or not verify the scope of the keyword and causes a value that is outside the range of the data type produces a running exception. Specifically, if the value produced by an integer evaluation statement that is not in the validation or validation of the keyword range is outside the allowable range of the data type, and/checked+ (/checked) is used in the compilation, the statement will produce an exception at run time, if/checked-is used at compile time, The statement does not produce an exception at run time.

Example: Compiling MyMath.cs and specifying an integer evaluation statement that is not in the scope of the validation or validation of a keyword (and whose resulting value is outside the range of the data type) will cause an exception at run time.

Csc/checked+ MyMath.cs

Seven /codepage

If the user compiles one or more source code that does not use the default code page on the computer, you can use the/codepage option to specify the code page that you want to use. /codepage applies to all source code files in the compilation.

If the source code file is created in the same Code page location on the computer, or if the source code file is created with Unicode or UTF-8, the user does not need to use/codepage.

Eight /debug

This option is used during debugging, and when the debugger enables this option to debug its own program, a. pdb file is created and a variety of debugging information is written to this file. There are 2 options to specify the type of debugging:

/debug [+ +]: Elected with/debug + will create the. pdb file, and store the debug information inside;/debug-is a default setting that does not produce any debugging information.

/debug:[full/pdbonly]: When using/debug:full is to create default debugging information, a bit like the/debug+ option. The/debug:pdbonly option is to create a. pdb file, and you can only use source code debugging in the Debugging tools.

Example: Compiling Hello.cs and creating debug information for Hello.cs

csc/debug+ HelloWorld.cs
Nine /define

This option defines a symbol in the program, and he uses the #define preprocessor in the source program to indicate the same functionality, which remains in the defined state until the #undef indicator in the source file deletes the definition or the compiler has reached the end of the file. You can use/d shorthand instead.

Example: The following is the My.cs source program

Using System;
public class mybuild{
public static void Main () {
#if (Final)
Console.WriteLine ("Final Build");
Console.WriteLine ("Trial Build");

If you compile with csc/define:final My.cs, the final build is displayed, and if there is no/define, then the compile execution will display the "Trial builds".

Ten /doc

Documents are becoming more and more important today, and a good program should have quite a lot of documentation. If you are using the "///" identifier in the document where you write the program, annotate it. When you compile with the/doc option, your annotation document will automatically remain in an XML file.

Example: The following is the My.cs source program

Using System;
This is a sample class that has some documentation
public class MyDocument {

Main entry point of the class
public static void Main (string[] argv)
Console.WriteLine ("A Sample Class");

Using the following compile statement produces a my.xml file to see what is stored in the My.xml file.

Csc/doc:my.xml My.cs

By default, compilation errors or warnings only indicate the name of the file where the error was found, and adding this option will show the full path when the compiler generates an error or warning. You can make the above My.cs program syntax wrong, and then use Csc/fullpaths My.cs and CSC My.cs separately compiled to see how the error hint is different.


This option is primarily to activate the incremental compiler, which compiles only those functions that have changed since the last compilation. If the/debug option is selected at compile time, the status of the debug information is stored in the appropriate. pdb file. In addition to this compile-time information is stored in the. incr file, the name of this. incr file is output_file_name.extension.incr. That is, if the output file is Out.exe, the corresponding incr file for this file is the out.exe.incr file.

Example: Compiling files with an incremental compiler

Csc/incremental/out:my.exe My.cs

If the compilation succeeds, it will produce 2 files, respectively: My.exe and MY.EXE.INCR.


This option is created in the output file. NET resource. His shorthand is/linkres. Resource files are all the resources that are used in engineering files, such as pictures, sounds, and so on. This option simply establishes a link to the resource file, which helps manage programs that use the same resource without requiring multiple replicas. The specific syntax for this option is as follows:



FileName: is to establish a link. NET resource file

Identifier (optional): The logical name of the resource, which is used to load resources, and the default name is the file name.

MimeType (optional): is a string representing the media type of the resource. The default is empty.

Example: Create a link to reso.resource in a file

Csc/linkres:reso.resource MyResource.cs


When we compile two or more classes that have the main method, we can use this option to let the user specify the method in the final output file that uses that main.

Example: compiling two files, but the main method in the output file is from Main1 Class

csc MyMain1.cs mymain2.cs/main:main1

This option disables display of the start flag when the compiler starts and displays the report information during compilation.


Csc/nologo My.cs


Compiles the file, but does not create any output files. Users can see any compilation errors and warnings.


Csc/nooutput My.cs


This option prohibits importing mscorlib.dll. This DLL contains the System namespace. This option is typically used when users want to use their own system namespaces.

Example: compiling a file but not importing MSCORLIB.DL

Csc/nooutput MyOutput.cs


This option suppresses the specified warning type during compilation. If you are prohibiting multiple warning types, separate them with commas.
Example: Suppress warning types during compilation CS0108 and CS0109

csc/nowarn:108,109 Warn.cs


This option activates or disables optimizations performed by the compiler. The result of optimization is to make the output file smaller, faster, and more efficient. By default,/optimize performs optimizations, and if you choose/optimize-, you prohibit optimization. /O is a shorthand for/optimize.

Example: compiling files, and preventing optimizations



In the case where the output file is not specified, if the file is an EXE after compiling through the compiler, the output file obtains a name from the file containing the source code of the Main method, and if the compiled file is a DLL file, the name is obtained from the first source file. This option is available if the user wants to specify the output file name.

Example: Compiling the HelloWord.cs file and naming the output file as Hello.exe

Csc/out:hello.exe HelloWorld.cs

This option allows the user to compile all source code files in the specified directory or in a subdirectory of the engineering directory. The user can use wildcard characters to compile all matching files in the engineering directory.

Example: Compile all C # files in the/DIR1/DIR2 directory and its subdirectories, and generate Dir2.dll



This option enables the current compilation project to use the public type information in the specified file. This option is very important for beginners. The abbreviation for this option is/R. You must refer to all files imported using the "using" keyword in your program code, and if you use your own class library in your program, you must refer to it at compile time.

Example: compiling a file and referencing the file used in the program

Csc/r:system.dll;myexec.exe;mylibrary.dll MyProject.cs

(Note: One of the MyExec.exe and MyLibrary.dll created it himself)


This option is to tell the compiler what type of output file you want to get. Unless you use the/target:module option, the output files created by the other options contain the assembly list. The Assembly list stores information about all the files in the compilation. If multiple output files are generated on one command line, only one assembly list is created and stored in the first output file.

Here are 4 ways to use/target:

/target:exe to create an executable (EXE) console application

/target:library Create a code base (DLL)

/target:winexe Create a Windows program (EXE)

/target:module Create a module (DLL)


Csc/target:exe MyProj.cs//Create an EXE file

Csc/target:winexe myProject.cs file://Create a Windows program

Csc/target:library MyProject.cs file://Create a code base

Csc/target:module myProject.cs file://Create a module


This option is just the opposite of/linkresource. His role is to put. NET resource files in the output file, parameters, usage and/linkresource are the same, the specific reference to the previous/linkresource options.

This option tells the compiler to compile the file in unsafe mode

Example: Compiling My.cs in unsafe mode

Csc/unsafe My.cs


Using this option is the level of warning that is used during the compilation process

Warning Level implications
0 Turn off all warnings
1 Show only severe warnings
2 Level 1 warnings and some not-so-serious warnings
3 level 2 warnings and some not very serious warnings
4 Level 3 Warning and informational warning

Example: compiling a file without displaying any errors

csc/warn:0 My.cs


Tell the compiler to treat all warnings as errors in the compilation. /warnaserror-is the default option, in which the warnings in compilation do not affect the output of the file. /warnaserror and/warnaserror+ are the same.

Example: Compiling a file and taking warnings as errors in the compilation

Csc/warnaserror Myj.cs


Inserts an icon file (. ico) into the output file. The file identified with this icon is visible in the resource manager in Windows.


Csc/win32icon:myicon.ico My.cs


Adds a Win32 resource file to the output file. This resource file includes version information or bitmap (icon) information for the user application. If the user does not specify/win32res, the compiler generates version information from the assembly version.

Example: Add a Win32 resource file to the output file

Csc/win32res:winrf.res Mt.cs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.