How to Use csc.exe to compile Visual C # code files

Source: Internet
Author: User
Tags mscorlib

Visual C # is a new-generation programming language launched by Microsoft, and Visual C # is Microsoft. NET
An important component of the framework is a new development platform that Microsoft strongly recommends to programmers. Compared with previous development languages, he has more powerful functions and higher stability.
Qualitative and higher security. Because of this, Visual C # is becoming popular with more and more programmers.

Visual
The C # compiler is significantly different from the compiler used in programming languages. The biggest difference is that in the past, the program compiler compiled the program code to generate a machine language that can be directly used by the computer.
Statement. Although visual
The C # compiler can also compile compiled program code into an EXE or DLL file. However, this file is only an il file (intermediate language) and cannot be directly used by computers. Only when
When this il file is called, a machine code called JIT (instant compilation) compiler generates this il file for computer use.

Visible visual
The second part,
From the Il file to the machine language, this project is automatically implemented by machines. This document describes how to use csc.exe to correctly compile the Visual C # file.

Csc.exe
C # When program code is compiled into an il file, there are many parameters and switch options. Correct understanding and use of these parameters and switches sometimes solve seemingly tricky problems. The following describes a table.
The specific functions of these parameters and switches. These parameters and switch options are listed alphabetically. "*" Is a common parameter or switch.

Option usage
@ * Specify the response file.
/?, /Help display compiler options on the computer screen of the console
/Addmodule specifies one or more modules as part of the Assembly
/Baseaddress: Specify the base address for DLL loading.
/Bugreport: Creates a file that contains information that is easier to report errors.
/Checked if the integer calculation overflows the Data Type Boundary, an exception event is generated at runtime.
/CodePage specifies the code page for all source code used during compilation
File
/Debug * Send debugging information
/Define defines the program symbols for preprocessing.
/DOC * annotate the processed document as an XML file
/Fullpaths specifies the path to compile the output file
/Incremental incremental compilation of source code files
/Linkresource
Link to collection
/Main specifies the location of the main method
/Nologo: indicates that the compiler is forbidden.
/Nooutput: Compile the file but not output the file.
/Nostdlib does not export the standard library (that is, mscorlib. dll)
/Nowarn compilation, but the compiler does not display the warning function
/Optimize enable or disable Optimization
/Out * specifies the output file
/Recurse search for the subdirectory of the compiled source file
/Reference * import metadata from a file containing a collection
/Target * specifies the output file format
/Unsafe compile code that uses non-security keywords
/Warn set warning level
/Warnaserror escalation warning to error
/Insert win32icon into A. ICO file to export the output file
/Win32res insert a Win32 resource export output file

Details:

I .@

  
This option is used to specify the response file. A response file contains many compilation options. These compilation options will be processed by the compiler together with the source code file. Generally, the response file is a text
File format. The extension is. RSP. In the response file, the Comment starting with the # symbol is used.

For example, the content of a response file resp1.rsp is as follows:

# This is a simple response file named resp1.rsp

# Usage: CSC @ resp1.rsp

/Target: EXE/out: sample.exe sample. CS

  
Compile the sample.csfile into the sample.exe file. If you want to specify multiple response files in one compilation, you can specify multiple response file options, such:

@ File1.rsp @ file2.rsp

Ii ./? And/help

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

Iii./addmodule

  
This option enables the compiler to collect information of the type from the project being compiled by the user to the available files. All modules added with the/addmodule must be in the same directory as the output file at runtime. This is
You can specify a module in any directory during compilation, but this module must be in the application directory at runtime. The file cannot contain the Assembly List. For example
/Taarget: the metadata of the module can be imported using/addmodule.

Example: Add two modules to myproject. CS

CSC/addmodule: module1.dll; module2.dll myproject. CS

4./baseaddress

  
This option allows you to specify the preferred address for DLL loading. The preferred address can be decimal, hexadecimal, or octal. The default preferred DLL address is set at. Net runtime. If the target file is not
DLL file, this option will be ignored.

Example: Compile the mylibrary. cs dll file and the address when the DLL is loaded in the. NET runtime environment is 0x1111000.

CSC/baseaddres: 0x1111000/Target: Library mylibrary. CS

5./bugreport

This option is used to report errors during compilation. The report contains the following content:

1). One copy of all source code during compilation

2). compile all the compilation options

3). Compile information, including the compiler, runtime, and operating system version.

4). compiler output

5). Problem Description

6). Description of how to solve the problem

Example: generate a bugs.txt file and put the Error Report in the file

CSC/bugreport: bugs.txt hello. CS

6./checked

  
This option specifies whether an integer calculation statement that is out of the scope of the validation or not verified keywords and that causes a value beyond the range of data types to generate an exception for running. Specifically, if the keyword is not verified or not verified
The value generated by an integer calculation statement within the specified range is out of the permitted range of the Data Type and/checked + (/checked) is used during compilation. This statement generates exceptions during runtime, if
When/checked-is used during compilation, this statement will not produce exceptions during runtime.

  
Example: Compile mymath. CS, and specify an integer calculation statement that is not within the scope of the validation or unverified keywords (and the value generated exceeds the range of the data type), which will cause exceptions at runtime.

CSC/checked + mymath. CS

VII./codePage

  
If one or more source codes compiled by the user do not use the default code page on the computer, you can use the/codePage option to specify the code page to be used. /CodePage is applicable to compiling
All source code files.

If the source code file is created at the same code page on the computer, or the source code file is in Unicode or UTF-8
You do not need to use/codePage.
.

8./debug

  
This option is used during debugging. When the debugger enables this option to debug his program, it will create a. PDB file and write various debugging information to this file. There are 2 options to specify the debugging
Type:

/Debug [+/-]: The. PDB file is created and the debugging information is stored in the/debug + file./debug
-It is a default setting, that is, no debugging information is generated.

/Debug: [full/pdbonly]
: When/debug: Full is used, the default debugging information is created, which is somewhat similar to the/debug + option. /Debug:
The pdbonly option is to create a. PDB file, and you can only use source code debugging in the debugging tool.

Example: Compile hello. CS and create debugging information for hello. CS.

CSC/debug + helloworld. CS

9./define

  
This option defines a symbol in the program, which is the same as the # define Preprocessing Program Directive in the source program. This symbol remains in the defined State until the # UNDEF indicator in the source file is deleted.
The definition or compiler has reached the end of the file. You can use the/d abbreviation instead.

Example: The source program of my. CS

Using system;
Public class mybuild {
Public static void main (){
# If (final)
Console. writeline ("final build ");
# Else
Console. writeline ("trial build ");
# Endif
}
}

If you use CSC/define: Final my. CS for compilation, "final" is displayed.
Build ". If no/define is available," trial build "is displayed after compilation ".

10./doc

  
Documents have become increasingly important today. A good program should be equipped with equivalent documents. If you use the "//" identifier to comment in the program writing document. When you use the/DOC option to compile
Your comments will be automatically stored in an XML file.

Example: The source program of my. CS

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 ");
}
}

Use the following compilation statement to generate the my. xml file and check what is stored in the my. xml file.

CSC/DOC: My. xml my. CS

11./fullpaths

  
By default, compilation errors or warnings only indicate the names of files with errors found. This option is used to display the complete path when the compiler generates errors or warnings. You can
My. CS program syntax is wrong, and then use CSC/fullpaths my. CS and CSC my. CS to compile them separately to see what the error message is different.

12./Incremental

  
This option is mainly used to activate the incremental compiler. This compiler only compiles functions that have changed since the last compilation. If the/debug option is selected during compilation, the status of the debugging information is stored in the corresponding
In the. PDB file. The information during compilation is stored in the. incr file. The name of this. incr file is
Output_file_name.extension.incr. If out.exe is output, the incr file corresponding to this file is
Out.exe. incr file.

Example: Use the incremental compiler to compile files

CSC/Incremental/out: my.exe my. CS

If the compilation is successful, two files will be generated: my.exeand my.exe. incr.

Thirteen./linkresource

  
This option is used to create a link to the. NET Resource in the output file. The abbreviation is/linkres. Resource files are all resources used in engineering files, such as images and sounds.
This option only creates a link to the resource file, which helps you manage programs that use the same resource without multiple replicas. The syntax of this option is as follows:

/Linkresource: filename, identifier, mimetype

Where:

Filename: the. NET Resource file to be linked.

Identifier (optional): Logical name of the resource. The name is used to load the resource. The default name is the file name.

Mimetype (optional): a string that represents the media type of the resource. The default value is null.

Example: create a link to reso. Resource in the file.

CSC/linkres: reso. Resource myresource. CS

Fourteen./main

When we compile two or more classes with the main method, we can use this option to allow users to specify the main method used in the final output file.

Example: Compile two files, but the main method in the output file comes from main1 class

CSC mymain1.cs mymain2.cs/main: main1

15th./nologo

This option prohibits the compiler from displaying the start flag and report information during compilation at startup.

Example:

CSC/nologo my. CS

Sixteen./nooutput

Compile the file, but do not create any output file. You can see any compilation errors and warnings.

Example:

CSC/nooutput my. CS

17./nostdlib

This option prohibits the import of mscorlib. dll. This dll contains the system namespace. This option is generally used when you want to use your system namespace.

Example: Compile the file, but do not import mscorlib. DL

CSC/nooutput myoutput. CS

18./nowarn

This option disables the specified Warning type during compilation. If multiple warning types are not allowed, separate them with commas.
Example: Disable the warning types cs0108 and cs0109 during compilation

CSC/nowarn: 108,109 warn. CS

19th./optimize

  
This option activates or disables optimization by the compiler. The optimization result is that the output file is smaller, faster, and more efficient. The default value is/optimize. If you select
/Optimize-optimization is disabled. /O is short for/optimize.

Example: Compile the file and disable Optimization

CSC/optimise-my. CS

20./out

  
If the output file is an EXE file after compilation by the compiler, the name of the output file will be obtained from the file containing the source code of the main method; if the compiled file is
DLL file. The name is obtained from the first source code file. If you want to specify the name of the output file, you can use this option.

Example: Compile the helloword.csfile and name the output file hello.exe

CSC/out: hello.exe helloworld. CS

21./recurse

This option allows you to compile all source code files in the subdirectories of a specified directory or project directory. You can use wildcards to compile all matching files in the project directory.

Example: compile all C # files in the/dir1/dir2 directory and its sub-directories and generate dir2.dll

CSC/Target: Library/out: dir2.dll/recurse: dir1/dir2/*. CS

22./refrence

  
This option allows the current compilation project to use the public type information in the specified file. This option is very important for beginners. The abbreviation of this option is/R. You must reference using "using" in program code"
All files imported by keywords. If you use a self-compiled class library in your program, it must be referenced during compilation.

Example: compile a file and reference the file used in the program

CSC/R: system.dllw.myexec.exe; mylibrary. dll myproject. CS

(Note: The myexec.exe and mylibrary. DLL files are created by yourself)

23./Target

  
This option tells the compiler what type of output file you want. Unless the/Target: module option is used, the output files created by other options contain the Assembly List. Assembly List Storage
Information of all files in compilation. If multiple output files are generated in a command line, only one assembly list is created and stored in the first output file.

The following are four usage methods of/target:

/Target: EXE: Create an executable (exe) console application

/Target: Create a library (DLL)

/Target: winexe create a Windows program (exe)

/Target: module creates a module (DLL)

Example:

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 library

CSC/Target: module myproject. CS file: // create a module

Twenty-four./Resource

  
This option is opposite to/linkresource. The function is to embed the. NET Resource file into the output file. The parameters and usage are the same as those of/linkresource.
Refer to the previous/linkresource options.
25./unsafe

This option tells the compiler to compile files in unsafe mode.

Example: compile my. CS in unsafe mode

CSC/unsafe my. CS

26./warn

This option indicates the level of warning used during compilation.

Warning level description
0 close all warnings
1. Only Severe warnings are displayed.
2-Level 1 warning and some non-serious warnings
2 for level 3 and some not very serious warnings
Level 4 warning and information warning for level 3

Example: Compile the file without displaying any errors

CSC/warn: 0 my. CS

Twenty-seven./warnaserror

  
Tell the compiler to handle all warnings as errors during compilation. /Warnaserror-is the default option. Warnings during compilation under this option do not affect file output.
/Warnaserror and/warnaserror + are the same.

Example: Compile the file and treat the warning as an error during compilation.

CSC/warnaserror myj. CS

Twenty-eight./win32icon

Insert an icon into the output file
File (. ICO ). To manage resources in Windows
The file identified by this icon is displayed in the handler.

Example:

CSC/win32icon: myicon. ICO my. CS

19th./win32res

  
Add a Win32 resource file to the output file. This resource file contains the version information or Bitmap (icon) information of your application. If you do not specify/win32res, the compiler will
Generate version information based on the assembly version.

Example: Add a Win32 resource file to the output file.

CSC/win32res: winrf. Res Mt. CS

To compile all the options of csc.exe in the C # file. Understanding and understanding these options is sometimes very useful for programming.

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: info-contact@alibabacloud.com 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.