VC6 專案設定 (Project → Settings) 直接對應 `cl.exe` (編譯) 與 `link.exe` (連結) 命令列參數。理解各選項含義,能精準控制優化、偵錯、相容性、輸出體積,解決疑難雜症。
| 設定 | 旗標 | 說明 |
| Runtime Library | /MDd /MTd /MD /MT | Debug/Release × 動態/靜態 CRT,**全專案必須一致** |
| Calling Convention | /Gd /Gr /Gz | __cdecl / __fastcall / __stdcall |
| Struct Member Alignment | /Zp1 /Zp4 /Zp8 | 1/4/8 位元組對齊,**網路封包/共享記憶體必設** |
| Enable String Pooling | /GF | 合併相同字串常數 (唯讀) |
| Enable Function-Level Linking | /Gy | 函式級 COMDAT,配合連結器 /OPT:REF |
| Whole Program Optimization | /GL (VC7+) | 跨模組優化 (LTCG) |
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
| 設定 | 旗標 | 說明 |
| Base Address | /BASE:0x00400000 | EXE 預設 0x400000,DLL 0x10000000,避免重定位 |
| Entry Point | /ENTRY:MyEntry | 自訂進入點 (需 __stdcall),通常留空 |
| Stack Allocations | /STACK:reserve[,commit] | 預設 1MB,大遞迴需調大 |
| Version | /VERSION:1.0 | 嵌入 PE 標頭 |
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 同理
四、實戰小技巧 (避坑指南)
- 專案損壞/編譯器崩潰:刪除 `.ncb .opt .aps .clw` 及 `Debug/Release` 目錄重建
- 分享原始碼專案:同上刪除中間檔,只保留 `.dsp .dsw .h .cpp .rc` 等
- Workspace 多專案切換顯示:Tools → Options → Format → Workspace window → 字型設為 Fixedsys
- 專案重命名:關閉專案,文字編輯器開啟 `.dsp` 替換專案名稱
- ClassWizard 失靈:刪除 `.clw .ncb`,重開專案 → ClassWizard → **Add All** → Rebuild All
- 64 位元相容:檢查指標截斷 (`/Wp64`)、`SetWindowLong` → `SetWindowLongPtr`、`GetWindowLong` → `GetWindowLongPtr`
- 預編譯標頭不生效:確認 `stdafx.h` 為第一個 `#include`,專案設定 `/Yu`,`stdaffx.cpp` 設定 `/Yc`
五、Debug 追蹤 MFC 框架流程 (進階)
- ClassWizard 加入虛擬函式 (`OnNewDocument`, `Serialize`, `OnFileOpen` 等)
- 設中斷點,Debug → 單步執行 (F11)
- 跳過無符號 MFC 核心代碼 (右鍵呼叫堆疊 → Show External Code 取消)
- 觀察呼叫鏈:`CWinApp::OnFileNew` → `CDocManager` → `CMultiDocTemplate::OpenDocumentFile` → `CDocument::OnNewDocument` → `DeleteContents` → `Serialize`
- 同法追蹤範例程式 (SCRIBBLE, DRAWCLI)
- 上一篇文章: VC++ 定時任務實作方案對比:WM_TIMER、可等待計時器、系統排程器三種模式優劣分析
- 下一篇文章: 解決 CComboBox 下拉列表框無法自動收回的問題
Copyright ©
程式碼之家 All Rights Reserved