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

Source: Internet
Author: User
Tags mscorlib
Visual C # is a new generation launched by Microsoft Program Development language. Visual C # is an important component of the Microsoft. NET Framework framework. It is also a new development platform that Microsoft recommends to programmers. Compared with the previous development languages, he has more powerful functions, higher stability and higher security. Because of this, Visual C # is becoming popular with more and more programmers.
Visual C # compiler and past Programming Language The compiler is significantly different. The biggest difference is that in the past, the program compiler compiled a program Code Compile and generate machine languages that can be directly used by computers. Although the Visual C # compiler can also compile the compiled program code into an EXE or DLL file, this file is only an il file (intermediate language ), this Il file cannot be directly used by computers. Only when this il file is called, a machine code called JIT (instant compilation) compiler generates this il file for computer use.

The second part, from the Il file to the machine language, is automatically implemented by the machine. This document describes how to use csc.exe to correctly compile the Visual C # file.

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

Option Purpose
@* Specify the response file.
/?, /Help Display compiler options on the computer screen of the console
/Addmodule Specify one or more modules as part of the Assembly
/Baseaddress Specify the base address for DLL Loading
/Bugreport Create a file that contains information that is easier to report errors
/Checked If integer computing overflows the Data Type Boundary, an exception event is generated at runtime.
/CodePage Specify the code page so that allSource codeFile
/Debug * Send debugging information
/Define Define program symbols for preprocessing
/DOC * Comment the processed document into an XML file
/Fullpaths Specify the anyway path of the compiled output file
/Incremental Incrementally compile source code files
/Linkresource Link. net resources to the Collection
/Main Location of the main method
/Nologo Indicates that the compiler is forbidden.
/Nooutput Compile the file but do not output the file
/Nostdlib Do not export the standard library (mscorlib. dll)
/Nowarn Compile but the compiler does not display the warning function
/Optimize Enable or disable Optimization
/Out * Specify the output file
/Recurse Search for subdirectories of compiled source files
/Reference * Import metadata from files containing the collection
/Target * Output file format
/Unsafe Compile code that uses non-security keywords
/Warn Set warning level
/Warnaserror Raise warning to error
/Win32icon Insert a. ICO file to export the output file
/Win32res Insert a Win32 resource export 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, such a response file is in the form of a text file. 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 means that you can specify the modules 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, if the output file is created using/taarget: module, the metadata 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 a DLL file, this option is 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 value produced by an integer calculation statement that is not within the range of the tested or unverified keyword is out of the permitted range of the data type, when/checked + (/checked) is used in compilation, this statement will generate exceptions during runtime. If/checked-is used during compilation -, this statement does not produce exceptions during running.

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 created using 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 creation will be performed when/debug + is selected. PDB file, and store debugging information in it;/debug-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 similar to/debug +. /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 that indicated by the # define Preprocessing Program in the source program. This symbol 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 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 CSC/define: Final my. CS is used for compilation, "final build" is displayed. 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 for compilation, 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 use the above my. CS program syntax to make a mistake, and then use CSC/fullpaths my. CS and CSC my. CS to compile them separately to see if 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. PDB file. The information during compilation is stored in the. incr file. The name of this. incr file is output_file_name.extension.incr. That is, if out.exeis output, the incrfile corresponding to this file is out.exe. incr.

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 a 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 all files imported using the "using" keyword in the program code. If you use a self-compiled class library in your program, you must also reference it 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. The Assembly list stores information about 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. Its function is to embed the. NET Resource file into the output file. The parameters and usage are the same as those of/linkresource. For details, refer to the previous/linkresource option.

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 Disable all warnings
1 Show only severe warnings
2 1-level warning and some non-serious warnings
3 2 warning and some not very serious warning
Level 4 Do not give 3 warnings and information warnings

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 file (. ico) into the output file ). The file marked with this icon is displayed in the Windows Resource Manager.

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 generates 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.

From: pcdog.com

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.