使用 csc.exe 实现命令行生成Command-line build with csc.exe

通过在命令提示符处键入 C# 编译器的可执行文件名称 (csc.exe ),可调用该编译器。You can invoke the C# compiler by typing the name of its executable file (csc.exe) at a command prompt.

如果使用“Visual Studio 开发人员命令提示” 窗口,系统将设置所有必需的环境变量。If you use the Developer Command Prompt for Visual Studio window, all the necessary environment variables are set for you. 有关如何访问此工具的信息,请参阅 Visual Studio 开发人员命令提示主题。For information on how to access this tool, see the Developer Command Prompt for Visual Studio topic.

如果使用标准命令提示符窗口,则必须调整路径,然后才能从计算机的任意子目录调用 csc.exe 。If you use a standard Command Prompt window, you must adjust your path before you can invoke csc.exe from any subdirectory on your computer. 还必须运行 vsvars32.bat 来设置适当的环境变量以支持命令行生成操作。You also must run vsvars32.bat to set the appropriate environment variables to support command-line builds. 有关 vsvars32.bat 的详细信息,包括它的查找和运行说明,请参阅如何:设置 Visual Studio 命令行的环境变量For more information about vsvars32.bat, including instructions for how to find and run it, see How to: Set Environment Variables for the Visual Studio Command Line.

如果你使用的计算机只安装有 Windows 软件开发工具包 (SDK),则可以在“SDK 命令提示符” 处使用 C# 编译器,该窗口可通过“Microsoft .NET Framework SDK” 菜单选项打开。If you're working on a computer that has only the Windows Software Development Kit (SDK), you can use the C# compiler at the SDK Command Prompt, which you open from the Microsoft .NET Framework SDK menu option.

也可以使用 MSBuild 以编程方式生成 C# 程序。You can also use MSBuild to build C# programs programmatically. 有关详细信息,请参阅 MSBuildFor more information, see MSBuild.

csc.exe 可执行文件通常位于 Windows 目录下的 Microsoft.NET\Framework\<Version> 文件夹中。The csc.exe executable file usually is located in the Microsoft.NET\Framework\<Version> folder under the Windows directory. 根据每台计算机上的具体配置,此位置可能有所不同。Its location might vary depending on the exact configuration of a particular computer. 如果计算机上安装了不止一个版本的 .NET Framework,您将发现此文件的多个版本。If more than one version of the .NET Framework is installed on your computer, you'll find multiple versions of this file. 有关此类安装的详细信息,请参阅如何:确定安装的 .NET Framework 版本For more information about such installations, see How to: determine which versions of the .NET Framework are installed.

提示

使用 Visual Studio IDE 生成项目时,可以在 “输出” 窗口显示 “csc” 命令以及与之关联的编译器选项。When you build a project by using the Visual Studio IDE, you can display the csc command and its associated compiler options in the Output window. 若要显示此信息,请按照如何:查看、保存和配置生成日志文件中的说明将日志数据的详细级别更改为“常规” 或“详细” 。To display this information, follow the instructions in How to: View, Save, and Configure Build Log Files to change the verbosity level of the log data to Normal or Detailed. 重新生成项目之后,在 “输出” 窗口中搜索 “csc” 即可找到所调用的 C# 编译器。After you rebuild your project, search the Output window for csc to find the invocation of the C# compiler.

在本主题中In this topic

C# 编译器的命令行语法规则Rules for command-line syntax for the C# compiler

在解释操作系统命令行上给出的参数时,C# 编译器使用下列规则:The C# compiler uses the following rules when it interprets arguments given on the operating system command line:

  • 参数用空白分隔,空白可以是一个空格或制表符。Arguments are delimited by white space, which is either a space or a tab.

  • 插入符号 (^) 未被识别为转义符或者分隔符。The caret character (^) is not recognized as an escape character or delimiter. 在传递给程序中的 argv 数组之前,该字符由操作系统中的命令行分析器处理。The character is handled by the command-line parser in the operating system before it's passed to the argv array in the program.

  • 无论其中有无空白,包含在双引号 ("string") 中的字符串均被解释为单个参数。A string enclosed in double quotation marks ("string") is interpreted as a single argument, regardless of white space that is contained within. 带引号的字符串可以嵌入在自变量内。A quoted string can be embedded in an argument.

  • 前面有反斜杠的双引号 (\") 被解释为原义双引号字符 (")。A double quotation mark preceded by a backslash (\") is interpreted as a literal double quotation mark character (").

  • 反斜杠按其原义解释,除非它们紧位于双引号之前。Backslashes are interpreted literally, unless they immediately precede a double quotation mark.

  • 如果偶数个反斜杠后跟双引号,则每对反斜杠中有一个反斜杠被置于 argv 数组中,而双引号被解释为字符串分隔符。If an even number of backslashes is followed by a double quotation mark, one backslash is put in the argv array for every pair of backslashes, and the double quotation mark is interpreted as a string delimiter.

  • 如果奇数个反斜杠后跟双引号,则每对反斜杠中有一个反斜杠被置于 argv 数组中,而双引号由剩余反斜杠进行“转义”。If an odd number of backslashes is followed by a double quotation mark, one backslash is put in the argv array for every pair of backslashes, and the double quotation mark is "escaped" by the remaining backslash. 这将在 argv 中添加一个原义双引号 (")。This causes a literal double quotation mark (") to be added in argv.

C# 编译器的示例命令行Sample command lines for the C# compiler

  • 编译生成 File.exe 的 File.cs :Compiles File.cs producing File.exe:
csc File.cs 
  • 编译生成 File.dll 的 File.cs :Compiles File.cs producing File.dll:
csc -target:library File.cs
  • 编译 File.cs 并创建 My.exe :Compiles File.cs and creates My.exe:
csc -out:My.exe File.cs
  • 编译当前目录中的所有 C# 文件,对其进行优化并定义 DEBUG 符号。Compiles all the C# files in the current directory with optimizations enabled and defines the DEBUG symbol. 输出为 File2.exe :The output is File2.exe:
csc -define:DEBUG -optimize -out:File2.exe *.cs
  • 编译当前目录中的所有 C# 文件,生成 File2.dll 的调试版本。Compiles all the C# files in the current directory producing a debug version of File2.dll. 不显示徽标和警告:No logo and no warnings are displayed:
csc -target:library -out:File2.dll -warn:0 -nologo -debug *.cs
  • 将当前目录中的所有 C# 文件编译为 Something.xyz (DLL):Compiles all the C# files in the current directory to Something.xyz (a DLL):
csc -target:library -out:Something.xyz *.cs

C# 编译器和 C++ 编译器输出之间的差异Differences between C# compiler and C++ compiler output

调用 C# 编译器时,不会创建任何对象 (.obj) 文件,而是直接创建输出文件。There are no object (.obj) files created as a result of invoking the C# compiler; output files are created directly. 因此,C# 编译器不需要链接器。As a result of this, the C# compiler does not need a linker.

请参阅See also