csc.exe 是 C# 命令列編譯器,支援數十種參數控制編譯行為。下表彙整核心參數(* 標示常用):
| 參數 | 用途 |
|---|---|
@* | 指定回應檔 (.rsp),包含多組編譯選項 |
/?, /help | 顯示編譯器選項說明 |
/addmodule | 將模組納入組件(非獨立組件) |
/baseaddress | 指定 DLL 載入基底位址 |
/bugreport | 產生錯誤回報檔案 |
/checked | 啟用整數溢位檢查(預設關閉) |
/debug* | 產生偵錯資訊 (.pdb) |
/define | 定義前置處理器符號 (等同 #define) |
/doc* | 產生 XML 文件檔 |
/keyfile / /keycontainer | 指定強名稱金鑰檔/容器 |
/main | 指定進入點 Main 方法所在類別 |
/optimize | 啟用/關閉程式碼優化 |
/out* | 指定輸出檔名稱 |
/reference (/r)* | 參考組件中繼資料 |
/target (/t)* | 指定輸出型別:exe、winexe、library、module |
/unsafe | 允許編譯 unsafe 程式碼 |
/warn | 設定警告等級 (0-4) |
/warnaserror | 將警告視為錯誤 |
/win32icon | 嵌入 .ico 圖示 |
/win32res | 嵌入 Win32 資源檔 (.res) |
/t:exe - 主控台應用程式/t:winexe - Windows GUI 應用程式(無主控台視窗)/t:library - 類別庫 (DLL),包含組件資訊清單/t:module - 模組 (DLL),不含 組件資訊清單,需搭配 /addmodule 使用/debug+ 或 /debug:full - 完整偵錯資訊,支援 JIT 偵錯/debug:pdbonly - 僅產生 .pdb,僅支援原始碼偵錯/debug- - 不產生偵錯資訊(預設)csc /r:System.dll;MyLib.dll MyApp.cs
若專案使用 using 引入命名空間,編譯時必須以 /r 參考對應組件。
csc /define:DEBUG;TRACE MyApp.cs
// 程式碼中可用 #if DEBUG ... #endif
程式碼中使用 /// 註解,編譯時加 /doc:output.xml 即可產生結構化文件。
建立 build.rsp:
# 回應檔範例
/target:library
/out:MyLib.dll
/r:System.Data.dll
/debug+
/optimize+
*.cs
編譯時僅需:csc @build.rsp