首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 程式編譯執行實戰:命令列編譯器 csc.exe 參數完整對照表與遞增編譯技巧

     編輯:關於C語言

執行第一支 C# 程式

理解代碼含義後,讓程式真正跑起來。Visual Studio 可直接 F5,無 VS 環境需安裝 .NET Framework SDK(內建 csc.exe 編譯器)。

命令列編譯基本流程

  1. 啟動命令提示字元
  2. 切換至原始碼目錄
  3. 執行:csc welcome.cs

正常情況:產生 welcome.exe,執行提示輸入姓名,回車後顯示歡迎訊息。

關鍵差異:C# 無連結器

C/C++:編譯 → 目標檔 .obj → 連結 → .exe/.dll

C#:原始碼直接編譯為執行檔或 DLL,單一步驟完成,csc.exe 內建連結功能。

常用編譯選項速查表

選項用途範例
/out:檔名指定輸出檔名csc /out:MyApp.exe welcome.cs
/target:exe產生控制台執行檔(預設)csc /target:exe welcome.cs
/target:winexe產生 Windows GUI 執行檔(無控制台視窗)csc /target:winexe App.cs
/target:library產生 DLL 類別庫csc /target:library Lib.cs
/target:module產生模組(無資訊清單,供 al.exe 組合)csc /target:module Mod.cs
/debug產生偵錯資訊(.pdb)csc /debug welcome.cs
/optimize啟用程式碼優化csc /optimize welcome.cs
/define:SYMBOL定義前置處理器符號csc /define:DEBUG welcome.cs
/reference:組件.dll參考外部組件csc /reference:Newtonsoft.Json.dll App.cs
/recurse:*.cs遞迴搜尋子目錄原始碼csc /recurse:*.cs
/nooutput僅語法檢查,不產出檔案csc /nooutput welcome.cs
/nowarn抑制警告訊息csc /nowarn welcome.cs
/warnaserror視警告為錯誤csc /warnaserror welcome.cs
/unsafe允許 unsafe 區塊編譯csc /unsafe UnsafeCode.cs
/doc:檔案.xml產生 XML 文件檔csc /doc:ApiDoc.xml Lib.cs
/win32icon:圖示.ico嵌入應用程式圖示csc /win32icon:app.ico App.cs
/win32res:資源.res嵌入 Win32 資源檔csc /win32res:app.res App.cs
/baseaddress:位址指定 DLL 偏好載入位址csc /baseaddress:0x40000000 Lib.cs
/bugreport:檔案產生錯誤回報檔csc /bugreport:report.txt App.cs
/codepage:ID指定原始碼編碼代碼頁csc /codepage:65001 App.cs
/fullpaths輸出絕對路徑csc /fullpaths App.cs
/incremental啟用遞增編譯csc /incremental App.cs
/main:類別名指定 Main 方法所在類別csc /main:Program App.cs
/nologo隱藏啟動版權資訊csc /nologo App.cs
/help 或 /?顯示所有選項csc /?

進階技巧

回應檔 @response.rsp

將常用選項寫入文字檔,編譯時 csc @response.rsp 套用,避免命令列過長。

遞增編譯 /incremental

僅重新編譯變更檔案,大型專案顯著縮短建構時間。

語法檢查模式 /nooutput

CI/CD 前置檢查:快速驗證語法無誤再進行完整建構。

完整選項請執行 csc /? 或查閱 MSDN 線上文件。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved