How to compile the Visual C # code file Visual C # compiler with the compiler of the Visual C #, and the compiler in the previous programming language is significantly different. The biggest difference is that the previous program compiler is a machine language that compiles the program code code code that can be used directly to the computer. Although the Visual C # compiler can also compile the written program code into an EXE or a DLL file, but this file is just an IL file (intermediate language), this IL file cannot be used directly by the computer. Just when this IL file is called, the IL file generates the machine code that can be used by a computer code called JIT (instant compilation) compiler.
It can be seen that the compilation process of Visual C # can be generally divided into two parts, the first part, from the program code to the IL file, this process is implemented by manual intervention, that is, by csc.exe; second part, from IL files to machine languages, this project is automatic. This article will specify the first part, which is how to compile the Visual C # file correctly with CSC.exe.
Csc.exe has many parameters and switch options when compiling Visual C # program code into IL files. Correct understanding and use of these parameters and switches sometimes solve some seemingly tricky problems. Below, the specific role of these parameters and switches will be roughly explained. These parameters and switch options are arranged in alphabetical order. Among them, "*" is some common parameters or switches.
Option Use @ * Specify a response file. / ?, / help Display the compiler's option / addModule on the console / addModule Specifies one or more modules to specify a part of the base address / bugreport to load the DLL, which is a report error. More easily information / checked If an integer calculates the boundary of the overflow data type, generate an exception of an exception / codePage specify code page at runtime to send debug information / define definition pre-processes in the compiled file / debug * Program Sign / DOC * Trims the Document Note to XML File / FullPaths Specifies the Ultimate Path / INCREMENTAL of the Compile Output File / INCREMENTAL for the source code / LinkResource to add the .NET resource link to the collection / main specified main method Location / NOLOGO Prohibits the compiler's flag information / nooutput compile file, but does not output file / NostDLIB does not export the standard library (ie Mscorlib.dll) / NOWARN compile but the compiler does not display the warning function / Optimize Open or close Optimization / OUT * Specifying the subdirector of the output file / recurse search compilation source file / Reference * Import metadata / target * Specify the format of the output file in the file containing the set / unsafe compiles using non-secure keywords / WARN Settings WARNT / WARNASERROR WARNING Insert a .ico file passage output file in the error / win32icon insertion / win32res inserts a Win32 resource (============================================================================================================================================================================================== ====================== specifically description:
One. @
This option is used to specify a response file. The response file is a file containing many compilation options. These compilation options will be processed by the compiler with the source code file. Generally speaking, this response file is in the form of text files. His extension is .RSP. In the response file, it is a comment that starts with a # symbol. Example: The following is a response file resp1.RSP content:
# This is a simple response file, the file name is Resp1.RSP
# 使用用 方法: csc @ Resp1.RSP
/ Target: EXE /OUT: Sample.exe Sample.cs
The role 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 not have to say more, those who have used the DOS program, probably use this option.
three. / addmodule
This option is information that enables the compiler to collect the engineering from the user to the available file. All modules added / addModule must be in the same directory with the output file at runtime. That is to say, users can specify modules in any directory at compile, but this module must be in the application directory at runtime. The assembly list cannot be included in the file. For example: If the output file is created with / Taarget: Module, its metadata can be imported with / addModule.
Example: Add two modules to myProject.cs
CSC /ADDModule :Module1.dll;Module2.dll myproject.cs
four. / BaseAddress
This option allows the user to specify the preferred address when the DLL is loaded. This preferred address can be a decimal, hexadecimal, octal. The default preferred address of the DLL is set at .NET runtime. If the target file is not a DLL file, this option will be ignored.
Example: Compile the MYLIBRARY.CS to the DLL file, and when this DLL is loaded when the .NET run environment is 0x1111000
CSC / BaseAddres: 0x1111000 / Target: library mylibrary.cs ====================================== ======================= May. / bugreport
This option uses to report error messages when compiling. In the report contains the following:
1). Compilation of all source code
2). All compile options in compilation
3) Compile information, including the edchar, runtime, version information of the operating system
4) Compiler output
5). Description
6). 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
six. / checked
This option specifies whether an integer calculating statement that is not inspected or or not inside the keyword and the value of the value that exceeds the value of the data type range produces a run exception. Specifically, if the value is not in the range of the value generated by the integer calculation statement generated within the inspection or not verified in the keyword, the statement is running in the compilation of / checked (/ checked). When an exception is generated, if it is used / checked when compiled, the statement does not generate an exception. Example: Compile MYMATH.CS and specify an integer calculating statement that is not inspected or not inside the keyword range (and the range of the value it produces is outside the data type) will cause exceptions at runtime.
CSC / Checked mymath.cs
Seven. / codepage
If the user compiled one or more source code does not use the default code page on the computer, you can use the / codepage option to specify the code page you want to use. / CodePage is suitable for all source code files in compilation.
If the source code file is created on the same code page location on the computer, or 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 in debugging. When the tester enables this option to debug your own program, a .pdb file will be created and write various debug information to this file. There are 2 options to specify the type of debug:
/ debug [ /-]: When elected / debug will create a .pdb file, and store the debug information to the inside; / debug - is a default setting, that is, no debugging information is generated.
/ debug: [full / pdbonly]: When using / debug: full is to create default debugging information, a bit similar / debug option. / debug: The pdbonly option is to create a .pdb file, and you can only use the source code to debug in the debug tool.
Example: Compiling Hello.cs and create debugging information for Hello.cs
CSC / Debug HelloWorld.cs ============================================ ========= Nine. / define
This option defines a symbol in the program, and he and the same #define preprocessor indication in the source program, this symbol remains defined, until the #undef indicator in the source file deletes or the compiler has arrived. The end of the file. You can use / d to replace it.
Example: The following is the source program of My.cs
Using system; public class mybuild {public static void main () {#IF (firm) Console.writeline ("final building"); #Else Console.writeline ("trial build"); #ENDIF}}
If you use csc / define: final my.cs, "Final Build" is displayed, if there is no / define, "Trial Build" will be displayed after compiling.
ten. / DOC
The document has become more and more important today, and a good program should be equipped with a fairly document. If you are using the "///" identifier in the documentation of the program. When you use the / doc option to compile, your comment document will be automatically kept in an XML file. Example: The following is the source program of My.CS
Using system; ////// this is a sample class what 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 compiled statements to generate my.xml files, see what the My.xml file is stored.
CSC /DOC: My.XML my.cs ================================================================================================================================================= / fullpaths
By default, compiled errors or warnings will only indicate that the wrong file name is found, and this option will make the full path when the compiler generates an error or warning. You can compile the above my.cs program syntax, then compile with CSC / FullPaths my.cs and csc my.cs, see what is different from the error prompt.
Twelve ./incremental
This option is mainly to activate the incremental compiler, which compiles only functions that have changed after the last compiled. If the / debug option is selected, the status of the debug information is stored in the corresponding .pdb file. In addition to this compilation, it is stored in the .incr file, this .INCR file name is Output_File_name.Extension.inCr. That is, if Out.exe is output, the INCR file corresponding to this file is an out.exe.incr file.
Example: Using the incremental compiler to compile files
CSC / Incremental /out:my.exe my.cs
If the compilation is successful, 2 files are generated, namely my.exe and my.exe.incr.
Thirteen ./linkResource
This option is a link to the .NET resource in the output file. His shorthand is / linkres. The resource file is all resources used in engineering documents, like images, sounds, and more. This option is just a link to resource files, which helps manage programs that use the same resource without multiple copies. The specific syntax of this option is as follows:
/ LinkResource: FileName, Identifier, Mimetype
among them:
FileName: Yes wants to build a link to the resource file.
Identifier: The logical name of the resource is used to load the resource. The default name is the file name.
MIMETYPE: is a string of a media type representing the resource. The default is empty.
Example: Create a link to reso.resource in the file
CSC /Linkres:reso.Resource MyResource.cs
14./main
When we compile two or more CLASS with the main method, we can use this option to give the user to specify the method of using that main output file.
Example: Compiling two files, but the main method in the output file comes from Main1 Class
CSC myma1.cs mymain2.cs / main: main1 ========================================= ======15./nologo This option is forbidden to display the report information during the start flag and the compilation process when the compiler is started.
example:
CSC / NOLOGO my.cs
16./nooutput
Compile files but do not create any output files. Users can see any compilation errors and warnings.
example:
CSC / nooutput my.cs
Seventeen ./nostdlib
This option is forbidden to import mscorlib.dll. This DLL contains this system namespace. This option is usually used when the user wants to use its own system namespace.
Example: Compilation file, but does not import mscorlib.dl
CSC / nooutput myoutput.cs
Eighth eight ./nowarn
This option is to disable the specified warning type during the compilation process. If you are forbidden, multiple warning types are prohibited, separated by commas. Example: No warning type CS0108 and CS0109 during the compilation process
CSC / NOWARN: 108, 109 Warn.cs
19./optimize
This option is activated or disabled by the compiler to perform optimization. The result of optimization is to make the output file smaller, faster, and more efficient. The default is / Optimize execution optimization if you choose / Optimize-, prohibit optimization. / o is a shorthand of / Optimize.
Example: Compile file and prohibit optimization
CSC / OPTIMISE- my.cs
Twenty ./out
If the file is not specified, if the file is compiled by the compiler, the file will get the name from the file containing the source code containing the main method; if the compiled file is a DLL file, will be from the first Get the name in the source code file. This option can be used if the user wants to specify the output file name.
Example: Compiling the HelloWord.cs file and name the output file Hello.exe
Csc /out:hello.exe helloworld.cs ========================================= === 21./Recurse
This option allows users to compile all source code files in the specified directory or engineering directory. Users can use wildcard to compile all matching files in the project directory.
Example: Compile / DIR1 / DIR2 directory and all C # files in its lower-level directory, and generate dir2.dll
CSC / Target: library /out:dir2.dll / recurse: DIR1 / DIR2 / *. CS
Twenty-two ./refrence
This option allows the current compilation engineering to use public type information in the specified file. This option is very important for beginners. This option is shorter is / R. You must reference all the files imported in the program code, if you use your own class libraries in your program, you must also be referenced when compiling.
Example: Compilation file and reference files used in the program
CSC /R :System.dll;myexec.exe;mylibrary.dll myproject.cs (Note: I created when myExec.exe and mylibrary.dll)
Twenty-three ./TARGET
This option is to tell the compiler what type of output files you want to get. The output files created by other options include the list of compilation unless the / target: module option is used. The assembly list stores information on all files in compilation. In a command line, if multiple output files are generated, only one assembly list is created and stored in the first output file.
The following is 4 of the Target:
/ Target: EXE Creates an Execrete Console Application
/ Target: Library Creates a Code Base (DLL)
/ Target: WINEXE Creates 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 base
CSC / Target: Module MyProject.cs File: // Create a module
Twenty-four ./Resource
This option and / linkresource are just the opposite. His role is to embed the .NET resource file into the output file, the parameters, usage and / linkResource are also the same, and the specific references can be referred to the previous / linkResource option. ==================================25./unsafe
This option is to tell the compiler to compile files in non-secure mode
Example: Compile MY.CS with non-secure mode
CSC / unsafe my.cs
Twenty-six./warn
Use this option to use what level of warning levels in the compilation process
WARNING level mean 0 Close All Warnings 1 Show a Warning and Some Unruly WARNING 3 Level 2 Warnings and Some Unusually Warnings Level 4 Warnings and Information Warnings
Example: Compile file, do not display any errors
CSC / WARN: 0 my.cs
Twenty-seven./warnaserror
Tell the compiler to handle all warnings as an error in compilation. / Warnaserror- is the default option, and the warning in the compiled system does not affect the output of the file. / Warnaserror and / WarnaserR are the same.
Example: Compiling files and make a warning as an error in compilation
CSC / Warnaserror Myj.cs
Twenty eight ./win32icon
Insert an icon file (.ico) in the output file. Thus, you can see the file identified by this icon in the resource manager in Windows.
example:
CSC /WIN32ICON: Myicon.ico my.cs
Twenty-nine ./win32res
Add a Win32 resource file to the output file. This resource file includes version information or bitmap (icon) information for user applications. If the user does not specify / win32res, the compiler will generate version information according to the assembly version.
Example: Add a Win32 resource file to the output file
CSC /WIN32RES :WINRF.RES MT.CS