For example, how to use csc.exe to compile the code file of Visual C,

Source: Internet
Author: User
Tags mscorlib

For example, how to use csc.exe to compile the code file of Visual C,

C # compilation process

 

 

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

 

  • Csc.exe compiler path: C: \ Windows \ Microsoft. NET \ Framework64 \ v4.0.30319
  • How to compile:

1. For ease of use, you can manually add the preceding directory to the Path environment variable.

2.using csc.exe to compile HelloWorld. cs is very simple. Open a command prompt and switch to the directory where the HelloWorld. cs file is stored. Enter the following command line:

Csc/r: system. dll HelloWorld. cs

 

Command Options:

 

#

Option

Remark

1

@

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. 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 as @ file1.rsp @ file2.rsp.

2

/? And/help

3

/Addmodule

This option enables the compiler to collect all types of information 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: Put myLibrary. cs is compiled into a DLL file, and when this DLL is in. when the Net runtime environment is loaded, the address 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

7

/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 two 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 # undefine 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 ");

Console. ReadLine ();

# Else

Console. WriteLine ("Trial Build ");

Console. ReadLine ();

# 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;

/// <Summary>

/// This is a sample class that has some documentation

/// </Summary>

Public class myBuild

{

/// <Summary>

/// Main entry point of the class

/// </Summary>

Public static void Main ()

{

}

}

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.

13

/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

14

/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

15

/Nologo

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

Example: csc/nologo my. cs

16

/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

The option is to disable 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

19

/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 all 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

24

/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 disables all warnings; 1 displays only severe warnings; 2 levels are 1 warnings and some are not serious warnings; 3-Level 2 warning and some not very serious warning; 4-Level 3 warning and information warning

Example: Compile the file without displaying any errors

Csc/warn: 0 my. cs

27

/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

28

/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

29

/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


How to Use CSCexe to compile Visual C # code files

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.

C # How to enable the command line compiler (cscexe?

Go to www.enet.com.cn/eschool/video/c/1.shtmlto check the video. How can I configure it?
There is also a lot of new knowledge

Related Article

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.