http://blog.csdn.net/gofishing/archive/2006/04/12/660798.aspx
今天在改一個舊的vc6工程(為了節省空間和方便備份,以前收尾時把中間文件都刪了),整理了一下文件,歸了歸類,結果不知怎么的,工程不能編譯了,編譯
什么文件都出現
fatal error C1083: Cannot open precompiled header file: 'Debug/xxx.pch': No such file or directory,
可是我也沒刪什么文件,改程序內容,就是把文件挪了挪,看來是vc的工程有點問題,只好查查怎么回事。
用vc新建的工程一般都有這兩個文件(空工程除外),其實如果不用mfc,這兩個根本沒用
StdAfx.cpp 就一句#include "stdafx.h"
StdAfx.h 包含了一些頭文件,如果沒用mfc就一句有用的#include <stdio.h>
要
是你把哪個源文件的#include "stdafx.h"這行刪除了,或者從其他工程考過來一個文件沒有這行的,編譯時一律是
fatal error C1010: unexpected end of file while looking for precompiled header directive,
這時你只要給該文件開頭加個#include "stdafx.h"就了事。
這兩個文件除了這點之外還有一個問題,就是這兩個文件不能從工程里刪
除,不然你編譯任何一個.cpp/.c的文件都會報
fatal error C1083: Cannot open precompiled header file: 'Debug/xxx.pch': No such file or directory,
你build也不行,vc會給每一個源文件報這樣一個錯誤。
你再把這兩個文件加入工程,一樣不行,不論你把他們加在哪,怎么編譯就是fatal error C1083,找不到.pch文件。只有新建一個工程,然后把原來的源文件都加過去,要是工程里有很多源文件,進行了很多設置的話,可就麻煩了。
于
是,我新建了一個工程就叫test把,看看怎么會出現fatal error C1083的。在新建一個工程后,vc在工程目錄里生成了幾個文件,包括
test.dsp(工程文件)、StdAfx.h、StdAfx.cpp。和主程序源文件test.cpp等。如果你build工程,會在相應的編譯目錄
下(一般為debug或release)產生test.pch、vc60.idb、vc60.pdb、StdAfx.obj和源程序的obj、pdb文件
和一個test.exe程序,fatal error C1083就是說沒有這個.pch文件,他也不能產生這個文件。那到底vc是怎么產生這個文件的,
又是作了什么改動使他不能再產生這個文件的呢?我們將debug目錄刪除,然后編譯test.cpp文件,結果j:\test
\test.cpp(4) : fatal error C1083: Cannot open precompiled header file: 'Debug/test.pch': No such file or directory;
不行,我build:
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
StdAfx.cpp
Compiling...
test.cpp
Linking...
test.exe - 0 error(s), 0 warning(s)
看
來是編譯StdAfx.cpp產生了test.pch,然后再編譯test.cpp的把;再把debug目錄刪了,編譯StdAfx.cpp就ok,看看
debug目錄:test.pch、StdAfx.obj、vc60.idb、vc60.pdb。原來是編譯StdAfx.cpp產生的pch文件,那從
工程里刪除這個文件然后在添加這個文件為什么不行呢?肯定是在在刪除/添加StdAfx.cpp文件過程中,工程文件不一樣了。先關閉工程,把
test.dsp備份,然后打開工程,刪除StdAfx.cpp文件,在添加,編譯,錯誤C1083,然后關閉工程(保存工程)。fc比較一下剛才備份的
dsp和現在的工程文件,發現原來正常的那個工程文件里在SOURCE=.\StdAfx.cpp之后多這么一
行# ADD CPP /Yc"stdafx.h",而刪了StdAfx.cpp再添加的工程就沒有,其他的就完全一樣。
正常的dsp文件包含StdAfx.cpp的這兩行是這樣的:
SOURCE=.\StdAfx.cpp
# ADD CPP /Yc"stdafx.h"
# End Source File
后記:
vc
真是奇怪呀,這行# ADD CPP /Yc"stdafx.h"在新建工程時有,從工程里刪除了StdAfx.cpp就沒有了,再在工程里加上這個
StdAfx.cpp就沒有加上。看似刪除再添加好像沒有動工程,誰知道他來這么一手,艾,要是懂編譯懂vc的還好,要是遇上我這樣的菜鳥,艾,只有和他
奮斗半天才把他搞得稀里糊涂。把他寫出來,免得和我一樣的菜鳥再費半天勁。
其實這個pch文件是預編譯頭
(PreCompiled Header),下邊是msdn里關于PreCompiled Header和/Yc選項的解釋。一直都是用集成編譯環境的,
先是tc,然后是vc,沒用過make,沒寫過makefile,對編譯、連接這些東西還不是很懂,高手見笑了。這個選項是可以在工程的設置里設的,具體
可以看下邊的msdn幫助。
/Yc (Create Precompiled Header File)
Home | Overview | How Do I | Compiler Options
This option instructs the compiler to create a precompiled header (.PCH) file that represents the state of compilation at a certain point. (To find this option in the development environment, click Settings on the Project menu. Then click the C/C++ tab, and click Precompiled Headers in the Category box.)
Command Line Project Settings Description
/Yc Create Precompiled Header File The compiler compiles all code up to the end of the base source file, or to the point in the base file where #pragma hdrstop occurs.
/Ycfilename Through Header The compiler compiles all code up to and including the .H file specified in the Through Header text box (filename).
The precompiled code is saved in a file with a name created from the base name of the file specified with the /Yc option and a .PCH extension. You can also use the /Fp option to specify a name for the precompiled header file.
If you use /Ycfilename (Through Header), the compiler compiles all code up to and including the specified file for subsequent use with the /Yu option.
Note
If the options /Ycfilename and /Yufilename occur on the same command line and both reference, or imply, the same file name, /Ycfilename, takes precedence. This feature simplifies the writing of makefiles.
Example
Consider the following code:
#include <afxwin.h> // Include header for class library
#include "resource.h" // Include resource definitions
#include "myapp.h" // Include information specific to this app
...
When this code is compiled with the command
CL /YcMYAPP.H PROG.CPP
the compiler saves all the preprocessing for AFXWIN.H, RESOURCE.H, and MYAPP.H in a precompiled header file called MYAPP.PCH.
See Also Creating Precompiled Header Files