cannot open precompiled header file 'Debug/xxx.pch'Unexpected end of file while looking for precompiled header directiveVC 使用 預編譯標頭 (Precompiled Header, PCH) 機制:
stdafx.cpp 僅含 #include "stdafx.h"stdafx.h 包含常用系統標頭 (afxwin.h, windows.h, vector...)stdafx.cpp 產生 xxx.pch (二進制語法樹快照)#include "stdafx.h" 即可直接載入 PCH,大幅加速編譯錯誤發生於:
#include "stdafx.h" (或順序錯誤)VS 會自動偵測 .pch 是否存在/有效,無效則重建。
#include "stdafx.h" (或保留亦無妨)Debug/、Release/、ipch/、*.pch、*.ncb、*.suo| 現象 | 原因 | 修正 |
|---|---|---|
| 新增 .cpp 忘記 #include "stdafx.h" | PCH 要求第一個 non-comment 行必須是此 include | 加在檔案最開頭 |
| stdafx.h 內容變更後未重建 | .pch 為舊版本 | 方案一或清理重建 |
| 多專案共用 stdafx.h 但 PCH 設定不一致 | 每專案獨立 PCH | 各專案單獨設定 |
| 使用 /clr (C++/CLI) 專案 | PCH 支援有限 | 建議停用 PCH |
若編譯/連結無錯但執行邏輯異常、單步偵錯跳行錯誤:
增量連結為了速度保留舊目標檔片段,偶爾導致符號表/行號表不同步。
stdafx.h → 最省心