Csc compiles c # files

Source: Internet
Author: User
Tags mscorlib

How to Use csc.exe to compile Visual C # code files
The Visual C # compiler is significantly different from the compiler 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. 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 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 files used during compilation
/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 compilation of source code files
/Linkresource links. NET resources to the 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, 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 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 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.

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.