csc.exe options

通常人们用IDE开发,很少关注编译细节,其实c#编译器有很多options, 了解它们,有时会帮你大忙。

1, 了解这些options的最简单方法,当然是查看帮助:

      进入csc.exe所在目录,通常在\WINDOWS\Microsoft.NET\Framework\[version] 下,比如 \WINDOWS\Microsoft.NET\Framework\v3.5

      然后,通过 key in csc.exe /help or csc.exe /? 你可以把这些说明redirect到一个文件中,方便随时查看,比如 redirect到当前folder:

      C:\WINDOWS\Microsoft.NET\Framework\v3.5>csc.exe /? > csc_options.txt

      然后用wordpad 打开 csc_options.txt,wordpad csc_options.txt

      你就会看到所有的options as below:

    

Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

                        Visual C# 2008 Compiler Options

                        - OUTPUT FILES -
/out:<file>                    Specify output file name (default: base name of file with main class or first file)
/target:exe                    Build a console executable (default) (Short form: /t:exe)
/target:winexe                 Build a Windows executable (Short form: /t:winexe)
/target:library                Build a library (Short form: /t:library)
/target:module                 Build a module that can be added to another assembly (Short form: /t:module)
/delaysign[+|-]                Delay-sign the assembly using only the public portion of the strong name key
/doc:<file>                    XML Documentation file to generate
/keyfile:<file>                Specify a strong name key file
/keycontainer:<string>         Specify a strong name key container
/platform:<string>             Limit which platforms this code can run on: x86, Itanium, x64, or anycpu. The default is anycpu.

                        - INPUT FILES -
/recurse:<wildcard>            Include all files in the current directory and subdirectories according to the wildcard specifications
/reference:<alias>=<file>      Reference metadata from the specified assembly file using the given alias (Short form: /r)
/reference:<file list>         Reference metadata from the specified assembly files (Short form: /r)
/addmodule:<file list>         Link the specified modules into this assembly

                        - RESOURCES -
/win32res:<file>               Specify a Win32 resource file (.res)
/win32icon:<file>              Use this icon for the output
/win32manifest:<file>          Specify a Win32 manifest file (.xml)
/nowin32manifest               Do not include the default Win32 manifest
/resource:<resinfo>            Embed the specified resource (Short form: /res)
/linkresource:<resinfo>        Link the specified resource to this assembly (Short form: /linkres)
                               Where the resinfo format is <file>[,<string name>[,public|private]]

                        - CODE GENERATION -
/debug[+|-]                    Emit debugging information
/debug:{full|pdbonly}          Specify debugging type ('full' is default, and enables attaching a debugger to a running program)
/optimize[+|-]                 Enable optimizations (Short form: /o)

                        - ERRORS AND WARNINGS -
/warnaserror[+|-]              Report all warnings as errors
/warnaserror[+|-]:<warn list>  Report specific warnings as errors
/warn:<n>                      Set warning level (0-4) (Short form: /w)
/nowarn:<warn list>            Disable specific warning messages

                        - LANGUAGE -
/checked[+|-]                  Generate overflow checks
/unsafe[+|-]                   Allow 'unsafe' code
/define:<symbol list>          Define conditional compilation symbol(s) (Short form: /d)
/langversion:<string>          Specify language version mode: ISO-1, ISO-2, or Default

                        - MISCELLANEOUS -
@<file>                        Read response file for more options
/help                          Display this usage message (Short form: /?)
/nologo                        Suppress compiler copyright message
/noconfig                      Do not auto include CSC.RSP file

                        - ADVANCED -
/baseaddress:<address>         Base address for the library to be built
/bugreport:<file>              Create a 'Bug Report' file
/codepage:<n>                  Specify the codepage to use when opening source files
/utf8output                    Output compiler messages in UTF-8 encoding
/main:<type>                   Specify the type that contains the entry point (ignore all other possible entry points) (Short form: /m)
/fullpaths                     Compiler generates fully qualified paths
/filealign:<n>                 Specify the alignment used for output file sections
/pdb:<file>                    Specify debug information file name (default: output file name with .pdb extension)
/nostdlib[+|-]                 Do not reference standard library (mscorlib.dll)
/lib:<file list>               Specify additional directories to search in for references
/errorreport:<string>          Specify how to handle internal compiler errors: prompt, send, queue, or none. The default is queue.
/moduleassemblyname:<string>   Name of the assembly which this module will be a part of

 

 

从这些说明,你会有一个简单的理解,下面进行一下稍微详细的说明:

 

一.@
  这个选项是用来指定响应文件。响应文件是一种包含了许多编译选项的文件。这些编译选项将和源代码文件一起由编译器进行处理。一般来说此种响应文件是以文本文件形式出现。他的扩展名是.rsp。在响应文件中是用#符号表示开始的注释。
  例:以下是一个响应文件resp1.rsp的内容:
  # 这是一个简单的响应文件,文件名称为resp1.rsp
  #使用方法: csc @resp1.rsp
/target:exe /out:sample.exe sample.cs
  此响应文件的作用就是把sample.cs文件编译成sample.exe文件。如果在一次编译中要指定多个响应文件,可以指定多个响应文件选项,如:
@file1.rsp @file2.rsp

Note: 我们可以看到在csc.exe的同级目录有一个csc.rsp文件,这个是默认的options文件,内容如下:

# This file contains command-line options that the C#
# command line compiler (CSC) will process as part
# of every compilation, unless the "/noconfig" option
# is specified.

# Reference the common Framework libraries
/r:Accessibility.dll
/r:Microsoft.Vsa.dll
/r:System.Configuration.dll
/r:System.Configuration.Install.dll
/r:System.Core.dll
/r:System.Data.dll
/r:System.Data.DataSetExtensions.dll
/r:System.Data.Linq.dll
/r:System.Data.OracleClient.dll
/r:System.Deployment.dll
/r:System.Design.dll
/r:System.DirectoryServices.dll
/r:System.dll
/r:System.Drawing.Design.dll
/r:System.Drawing.dll
/r:System.EnterpriseServices.dll
/r:System.Management.dll
/r:System.Messaging.dll
/r:System.Runtime.Remoting.dll
/r:System.Runtime.Serialization.dll
/r:System.Runtime.Serialization.Formatters.Soap.dll
/r:System.Security.dll
/r:System.ServiceModel.dll
/r:System.ServiceModel.Web.dll
/r:System.ServiceProcess.dll
/r:System.Transactions.dll
/r:System.Web.dll
/r:System.Web.Extensions.Design.dll
/r:System.Web.Extensions.dll
/r:System.Web.Mobile.dll
/r:System.Web.RegularExpressions.dll
/r:System.Web.Services.dll
/r:System.Windows.Forms.Dll
/r:System.Workflow.Activities.dll
/r:System.Workflow.ComponentModel.dll
/r:System.Workflow.Runtime.dll
/r:System.Xml.dll
/r:System.Xml.Linq.dll

 

这里是添加一般程序可能需要的引用,这些dll都是放在GAC(Global Assembly Cache)里面,让程序共用,当然你自己也可以把自己写的dll install 到GAC里面,注意,GAC里的dll必须是stong named。

GAC路径一般是C:\WINDOWS\assembly

 

  二./?和/help
  这个选项应该不必多说,那些用过DOS程序的人,大概都会用这个选项。
  三./addmodule
  本选项是使编译器搜集从用户正在编译的工程到可用文件中所以类型的信息。所有添加了/addmodule的模块在运行时必须与输出文件在同一目录中。这就是说,用户可以在编译时指定任何目录中的模块,但在运行时这个模块必须在应用程序目录中。文件中不能包含汇编名单。例如:如果输出文件用 /taarget:module创建,其元数据可以用/addmodule导入。
  例子:把二个模块加入myProject.cs中
csc /addmodule:module1.dll;module2.dll myProject.cs
  四./baseaddress
  本选项允许用户指定载入DLL时的首选地址,这个首选地址可以是十进制、十六进制、八进制。DLL的缺省首选地址在.Net运行时设置。如果目标文件不是DLL文件,这个选项将被忽略。
  例子:把myLibrary.cs 编译程DLL文件,并且当此DLL在.Net运行环境被载入时的地址是0x1111000
csc /baseaddres:0x1111000 /target:library myLibrary.cs
  五./bugreport
  这个选项用来报告编译时的错误信息。在报告中包含以下内容:
  1).编译中所有源代码的一个拷贝
  2).在编译中所有的编译选项
  3).编译信息,包括编译器、运行时间、操作系统的版本信息
  4).编译器输出
  5).问题的描述
  6).如何解决问题的描述
  例子:生成一个bugs.txt文件,并把错误报告放在文件里面
csc /bugreport:bugs.txt Hello.cs
  六./checked
  此选项指定不在检验或或者未检验关键字范围内以及导致超出数据类型范围的值的整数计算语句是否产生运行例外。具体的说就是,如果不在检验或者未检验关键字范围内的整数计算语句产生的值在数据类型允许的范围之外,并且在编译中使用了/checked+(/checked),该语句就会在运行时产生例外,如果在编译时使用了/checked-,在运行时该语句就不会产生例外。
  例子:编译myMath.cs,并且指定一个不在检验或者未检验关键字范围内的整数计算语句(且其产生的值超出数据类型的范围),将在运行时引起例外。
csc /checked+ myMath.cs
  七./codepage
  如果用户编译的一个或者多个源代码不使用计算机上的默认代码页,可以使用/codepage选项来指定希望使用的代码页。/codepage适用于编译中所有的源代码文件。
  如果源代码文件在计算机上的同一个代码页位置创建,或者源代码文件用UNICODE或者UTF-8来创建,用户就不需要使用/codepage了。
  八./debug
  此选项是在调试时候使用的,当调试者启用了这个选项来调试自己的程序,将会创建一个.pdb文件,并把各种调试信息写到此文件里。有2中选项来指定调试的类型:
  /debug [+/-] :当选用/debug +就会把创建.pdb文件,并把调试信息存储到里面;/debug -是一个缺省设置,就是不产生任何调试信息。
  /debug:[full/pdbonly] :当使用/debug:full就是创建缺省的调试信息,有点类似/debug+选项。/debug: pdbonly选项是创建.pdb文件,并且你只能使用源代码调试在调试工具里。
  例子:编译Hello.cs并且为Hello.cs创建调试信息
   csc /debug+ HelloWorld.cs
  九./define
  此选项在程序中定义了一个符号,他和在源程序中使用#define预处理程序指示功能相同,此符号保持已定义状态,直到源文件中的#undef指示符删除定义或者编译器已到达了文件末尾。你可以用/d简写来代替。
  例子:下面是my.cs的源程序
using System;
public class myBuild{
public static void Main() {
#if (final)
Console.WriteLine("Final Build");
#else
Console.WriteLine("Trial Build");
#endif
}
}
  如果用csc /define:final my.cs来编译就会显示"Final Build",如果没有/define,编译后执行就会显示"Trial Build"。
  十./doc
  文档在当今已经变得愈来愈重要了,一个好的程序应该配有相当的文档。如果你在写程序的文档中用的是"///"标识符来注释。当你使用/doc选项来编译时,你的所以注释文档将会自动的保留在一个XML文件中。
  例子:以下是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") ;
}
}
  用下列编译语句会产生my.xml文件,看看my.xml文件到底存储了什么东西。
Csc /doc:my.xml my.cs
  十一./fullpaths
  在默认情况下,编译产生的错误或者警告都只会指明发现错误的文件名称,加入此选项使得在编译器产生错误或者警告的时候会显示完整的路径。你可以把上面的 my.cs程序语法搞错,再用 csc /fullpaths my.cs 和 csc my.cs分别编译,看看错误提示有什么不同。
  十二./incremental
  本选项主要是激活增量编译器,这种编译器只对上次编译后发生改变的函数进行编译。如果在编译时候选用了/debug选项,调试信息的状态存储在相应的. pdb文件中。除此编译时的信息都存储在.incr文件中,此.incr文件的名称为output_file_name.extension.incr。即如果输出文件时out.exe,则此文件对应的incr文件是out.exe.incr文件。
  例子:利用增量编译器来编译文件
csc /incremental /out:my.exe my.cs
  如果编译成功则会产生2个文件,分别是:my.exe和my.exe.incr。
  十三./linkresource
  这个选项就是在输出文件中创建到.Net资源的链接。他的简写是/linkres。资源文件就是在那些在工程文件中使用到的所有的资源,像图片、声音等。这个选项只是对于资源文件建立链接,这样有助于管理使用同一资源的程序,而不需要多个副本。此选项的具体语法如下:
/linkresource:filename,identifier,mimetype
  其中:
  filename:是想建立链接的.Net的资源文件
  identifier(可选):资源的逻辑名称,该名称用于载入资源,默认名称是文件名称。
  mimetype(可选):是一个代表资源的媒介类型的字符串。默认为空。
  例子:在文件中建立一个指向reso.resource的链接
csc /linkres:reso.resource myResource.cs
  十四./main
  当我们编译二个或者多个拥有Main方法的Class,我们可以使用这个选项让用户指定最终的输出文件中的使用那个Main的方法。
  例子:编译二个文件,但输出文件中的Main方法来自Main1 Class
csc myMain1.cs myMain2.cs /main:Main1
  十五./nologo
  这个选项禁止在编译器启动时显示开始标志和编译过程中显示报告信息。
  例子:
csc /nologo my.cs
  十六./nooutput
  编译文件,但不创建任何输出文件。用户可以看到任何编译错误和警告。
  例子:
csc /nooutput my.cs
  十七./nostdlib
  这个选项禁止导入mscorlib.dll。这个DLL包含了这个系统名称空间。当用户希望使用自己的系统名称空间时,一般才会使用此选项。
  例子:编译文件,但不导入mscorlib.dl
csc /nooutput myOutput.cs
  十八./nowarn
  本选项是在编译过程中禁止指定的警告类型。如果是禁止多个警告类型,用逗号分隔。
例子:在编译过程中禁止警告类型CS0108和CS0109
csc /nowarn:108,109 Warn.cs
  十九./optimize
  本选项激活或者禁用由编译器执行优化。优化的结果是使得输出文件更小、更快、更有效率。缺省是/optimize执行优化,如果你选用了/optimize-则禁止优化。/o是/optimize的简写。
  例子:编译文件,并禁止优化
csc /optimise- my.cs
  二十./out
  在没有指定输出文件的情况下,如果通过编译器编译后文件是EXE文件,则输出文件将从包含Main方法的源代码的文件中获得名字;如果编译后的文件是DLL文件,将从第一个源代码文件中获得名字。如果用户想要指定输出文件名称,就可以使用此选项。
  例子:编译HelloWord.cs文件,并把输出文件命名为Hello.exe
csc /out:Hello.exe helloworld.cs
  二十一./recurse
  此选项允许用户编译在指定目录或者工程目录的所以子目录中的所有源代码文件。用户可以使用通配符来编译工程目录下的所有匹配文件。
  例子:编译/dir1/dir2目录下及其下级目录中的所有C#文件,并生成dir2.dll
csc /target:library /out:dir2.dll /recurse: dir1\dir2\*.cs
  二十二./refrence
  此选项可使得当前编译工程使用指定文件中的公共类型信息。这个选项对于初学者是很重要的。此选项的简写是/r。你必须引用在程序代码中使用"using"关键字导入的所有文件,如果在你的程序中,使用了自己编写的类库,在编译时也必须引用。
  例子:编译文件,并引用在程序中使用的文件
csc /r:system.dll;myExec.exe;myLibrary.dll myProject.cs
  (注:其中那个myExec.exe和myLibrary.dll时自己创建的)
  二十三./target
  这个选项是告诉编译器你所想得到什么类型的输出文件。除非使用/target:module选项,其他选项创建的输出文件都包含着汇编名单。汇编名单存储着编译中所有文件的信息。在一个命令行中如果生成多个输出文件,但只创建一个汇编名单,并存储在第一个输出文件中。
  下面是/target的4种用法:
  /target:exe 创建一个可执行(EXE)的控制台应用程序
  /target:library 创建一个代码库(DLL)
  /target:winexe 创建一个windows程序(EXE)
  /target:module 创建一个模块(DLL)
  例子:
  csc /target:exe myProj.cs // 创建一个EXE文件
  csc /target:winexe myProject.cs file://创建一个windows程序
  csc /target:library myProject.cs file://创建一个代码库
  csc /target:module myProject.cs file://创建一个模块
  二十四./resource
  此选项和/linkresource正好相反。他的作用是把.Net资源文件嵌入到输出文件中,参数、用法都和/linkresource也相同,具体可参考前面/linkresource选项。
  二十五./unsafe
  此选项是告诉编译器采用非安全模式编译文件
  例子:用非安全模式编译my.cs
csc /unsafe my.cs
  二十六./warn
  使用本选项是在编译过程中采用什么等级的警告级别
警告级别 含义
0 关闭所有警告
1 只显示严重警告
2 级别为1的警告和某些不严重的警告
3 级别为2的警告和某些不算非常严重的警告
4 级 别为3的警告和信息警告
  例子:编译文件,不显示任何错误
csc /warn:0 my.cs
  二十七./warnaserror
  告诉编译器把在编译中把所有的警告当成错误来处理。/warnaserror-是缺省选项,在该选项下编译中的警告不影响文件的输出。/warnaserror和/warnaserror+是一样的。
  例子:编译文件,并在编译中把警告当成错误
csc /warnaserror myj.cs
  二十八./win32icon
  在输出文件中插入一个图标文件(.ico)。从而在Windows中的资源管理器中就看到以此图标标识的文件了。
  例子:
csc /win32icon:myicon.ico my.cs
  二十九./win32res
  在输出文件中添加一个win32的资源文件。此资源文件包括用户应用程序的版本信息或者位图(图标)信息。如果用户不指定/win32res,编译器将根据汇编版本生成版本信息。
  例子:添加一个win32资源文件到输出文件中
csc /win32res:winrf.res mt.cs
  

 

likewise, 你可以同样查看vbc.exe 和 cl.exe(c/c++编译器,路径和csc, vbc不同,一般在Program Files\Microsoft Visual Studio 9.0\VC\bin)

 

  

 

posted @ 2010-05-09 13:45  alxe_yu  阅读(820)  评论(1编辑  收藏  举报