使用 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.exeIf 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 命令提示字元] (其可從 [Microsoft .NET Framework SDK] 功能表選項開啟) 中使用 C# 編譯器。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\ <版本> 資料夾中。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:

  • 引數會以空白或定位鍵的泛空白字元 (White Space) 進行分隔。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.exeFile.csCompiles File.cs producing File.exe:
csc File.cs 
  • 編譯可產生 File.dllFile.csCompiles File.cs producing File.dll:
csc -target:library File.cs
  • 編譯 File.cs 並建立 My.exeCompiles 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.exeThe output is File2.exe:
csc -define:DEBUG -optimize -out:File2.exe *.cs
  • 編譯產生 File2.dll 偵錯版本之目前目錄中的所有 C# 檔案。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