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

VC++ 編譯器與連結器深度解析:專案設定頁面全選項詳解、偵錯技巧與實戰最佳實踐

     編輯:關於VC++

前言:善用工具,事半功倍

VC6 專案設定 (Project → Settings) 直接對應 `cl.exe` (編譯) 與 `link.exe` (連結) 命令列參數。理解各選項含義,能精準控制優化、偵錯、相容性、輸出體積,解決疑難雜症。

一、編譯器設定 (C/C++ 分頁)

1. General (總體)

  • Warning Level:`/W3` (推薦) 或 `/W4` (嚴格)。`/WX` 視警告為錯誤
  • Optimization:`/Od` (Debug)、`/O2` (Release 速度)、`/O1` (Release 體積)
  • Debug Info:
選項旗標用途
None(無)最快編譯,無偵錯
Line Numbers Only/Zd僅行號,.obj 較小
C 7.0 Compatible/Z7舊式,資訊在 .obj
Program Database/Zi.pdb 檔,支援斷點/變數檢視
Edit and Continue/ZIDebug 預設,支援偵錯中修改代碼

2. C++ Language

  • Enable RTTI:`/GR` (MFC/動態轉型必開)
  • Exception Handling:`/GX` (同步) 或 `/EHa` (非同步/SEH)
  • Runtime Type Info:`/GR` 同 RTTI

3. Code Generation (關鍵)

4. Optimization (細項)

  • Global Optimizations (`/Og`):迴圈不變量外提、公共子運算式消除
  • Inline Expansion:`/Ob1` (僅 inline)、`/Ob2` (任何適合)
  • Favor Fast/Small Code:`/Ot` / `/Os`
  • Frame Pointer Omission:`/Oy` (Release 預設開)
  • Intrinsic Functions:`/Oi` (memcpy/strlen 等內建指令)

5. Precompiled Headers (關鍵加速)

  • Create/Use Precompiled Header:`/Yc"stdafx.h"` / `/Yu"stdafx.h"`
  • 專案屬性 → C/C++ → Precompiled Headers → **Use (/Yu)** 套用至全專案,**Create (/Yc)** 僅套用至 `stdafx.cpp`
  • `/FI` 強制包含 (等同專案層級 `#include`)

6. Preprocessor

  • Preprocessor Definitions:`/D WIN32 /D _DEBUG /D _CONSOLE` 等
  • Additional Include Directories:`/I ..\Include`
  • Ignore Standard Include Path:`/X`

二、連結器設定 (Link 分頁)

1. General

  • Output File:`/OUT:"Debug/MyApp.exe"`
  • Link Incrementally:`/INCREMENTAL` (Debug 預設,產生 .ilk,連結快但檔案大)
  • Ignore All Default Libraries:`/NODEFAULTLIB` (進階用)
  • Generate Map File:`/MAP` (分析體積/符號用)
  • Generate Debug Info:`/DEBUG` (產生 .pdb)

2. Input

  • Additional Dependencies:`kernel32.lib user32.lib ...` 額外 `.lib`
  • Ignore Specific Library:`/NODEFAULTLIB:libcmt.lib`
  • Force Symbol References:`/INCLUDE:_mySymbol` (強制保留)

3. Output

設定旗標說明
Runtime Library/MDd /MTd /MD /MTDebug/Release × 動態/靜態 CRT,**全專案必須一致**
Calling Convention/Gd /Gr /Gz__cdecl / __fastcall / __stdcall
Struct Member Alignment/Zp1 /Zp4 /Zp81/4/8 位元組對齊,**網路封包/共享記憶體必設**
Enable String Pooling/GF合併相同字串常數 (唯讀)
Enable Function-Level Linking/Gy函式級 COMDAT,配合連結器 /OPT:REF
Whole Program Optimization/GL (VC7+)跨模組優化 (LTCG)

4. Debug

  • Generate Debug Info:`/DEBUG`
  • Debug Info Format:`/PDB` (Microsoft) / `/PDB:COFF` / Both
  • Separate Types:`/PDB:SEP` (類型資訊獨立 .pdb,啟動較快)

三、其他重要設定頁

1. General (專案層級)

  • Microsoft Foundation Classes:**Use MFC in a Shared DLL** (`/MD` + `_AFXDLL`) 或 **Static Library** (`/MT`)。發布時攜帶 MFC DLL

2. Debug (偵錯屬性)

  • Executable for Debug Session:指定啟動程式 (DLL 專案指定宿主 EXE)
  • Program Arguments:命令列參數
  • Working Directory:工作目錄

3. Custom Build (建置後事件)

自動註冊 COM 元件:


Description: Register COM
Commands: regsvr32 /s /c $(TargetPath)
echo regsvr32 exe.time > $(TargetDir)\$(TargetName).trg
Outputs: $(TargetDir)\$(TargetName).trg

4. Tools → Options → Directories (全域)

  • Include files:`$(VCInstallDir)\include;$(WindowsSdkDir)\include;...`
  • Library files:`$(VCInstallDir)\lib;$(WindowsSdkDir)\lib;...`
  • Source files / Executable files 同理

四、實戰小技巧 (避坑指南)

  1. 專案損壞/編譯器崩潰:刪除 `.ncb .opt .aps .clw` 及 `Debug/Release` 目錄重建
  2. 分享原始碼專案:同上刪除中間檔,只保留 `.dsp .dsw .h .cpp .rc` 等
  3. Workspace 多專案切換顯示:Tools → Options → Format → Workspace window → 字型設為 Fixedsys
  4. 專案重命名:關閉專案,文字編輯器開啟 `.dsp` 替換專案名稱
  5. ClassWizard 失靈:刪除 `.clw .ncb`,重開專案 → ClassWizard → **Add All** → Rebuild All
  6. 64 位元相容:檢查指標截斷 (`/Wp64`)、`SetWindowLong` → `SetWindowLongPtr`、`GetWindowLong` → `GetWindowLongPtr`
  7. 預編譯標頭不生效:確認 `stdafx.h` 為第一個 `#include`,專案設定 `/Yu`,`stdaffx.cpp` 設定 `/Yc`

五、Debug 追蹤 MFC 框架流程 (進階)

  1. ClassWizard 加入虛擬函式 (`OnNewDocument`, `Serialize`, `OnFileOpen` 等)
  2. 設中斷點,Debug → 單步執行 (F11)
  3. 跳過無符號 MFC 核心代碼 (右鍵呼叫堆疊 → Show External Code 取消)
  4. 觀察呼叫鏈:`CWinApp::OnFileNew` → `CDocManager` → `CMultiDocTemplate::OpenDocumentFile` → `CDocument::OnNewDocument` → `DeleteContents` → `Serialize`
  5. 同法追蹤範例程式 (SCRIBBLE, DRAWCLI)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
設定旗標說明
Base Address/BASE:0x00400000EXE 預設 0x400000,DLL 0x10000000,避免重定位
Entry Point/ENTRY:MyEntry自訂進入點 (需 __stdcall),通常留空
Stack Allocations/STACK:reserve[,commit]預設 1MB,大遞迴需調大
Version/VERSION:1.0嵌入 PE 標頭