| 方式 | 機制 | 適用檔案 | 對 ClassView 影響 |
|---|---|---|---|
#include "file.h" | 前處理器文本替換,編譯期可見 | .h, .hpp, .inl | 無直接影響 |
| FileView → Add Files | 專案建置系統記錄,參與編譯/連結 | .cpp, .c, .rc, .def, .h (可選) | .h 中的類別會出現在 ClassView |
VC 解析專案中**所有列在 FileView 的 .h 檔案**,將其中的 class/struct/union 全部載入 ClassView 樹狀圖。大型專案若將數百個內部頭文件加入專案,ClassView 會變得極難導航。
#include 由 .cpp 引入即可;編譯器會自動尋找(專案目錄、Include 路徑)。resource.h、stdafx.h 等若被資源編譯器/預編譯頭直接引用,可保留在專案中。# 1. 移除不必要的 .h 在 FileView 中選中 .h → 右鍵 Remove (不刪除檔案,只移出專案) # 2. 確認 .cpp 都在專案中 FileView → Source Files → 右鍵 Add Files → 選擇所有 .cpp # 3. 設定 Include 路徑 (若 .h 不在專案目錄) Project → Settings → C/C++ → Preprocessor → Additional include directories 加入 .h 所在目錄
.dsp/.vcxproj 變小、版控差異減少。# CMakeLists.txt add_executable(MyApp src/main.cpp src/module.cpp # 不需列出 .h ) target_include_directories(MyApp PRIVATE include) # .h 所在目錄
現代建置系統**完全不需要**在專案列表出現 .h,僅靠 target_include_directories 指定搜尋路徑。
誤區:「.h 不加入專案會找不到」
事實:#include走的是編譯器 Include 搜尋路徑,與專案檔案列表無關。
誤區:「ClassView 顯示的類別越多越好」
事實:ClassView 是導航工具,雜訊過多反而降低生產力。