http://blog.csdn.net/gofishing/archive/2006/04/12/660798.aspx
今天在改一個(gè)舊的vc6工程(為了節(jié)省空間和方便備份,以前收尾時(shí)把中間文件都刪了),整理了一下文件,歸了歸類,結(jié)果不知怎么的,工程不能編譯了,編譯
什么文件都出現(xiàn)
fatal error C1083: Cannot open precompiled header file: 'Debug/xxx.pch': No such file or directory,
可是我也沒(méi)刪什么文件,改程序內(nèi)容,就是把文件挪了挪,看來(lái)是vc的工程有點(diǎn)問(wèn)題,只好查查怎么回事。
用vc新建的工程一般都有這兩個(gè)文件(空工程除外),其實(shí)如果不用mfc,這兩個(gè)根本沒(méi)用
StdAfx.cpp 就一句#include "stdafx.h"
StdAfx.h 包含了一些頭文件,如果沒(méi)用mfc就一句有用的#include <stdio.h>
要
是你把哪個(gè)源文件的#include "stdafx.h"這行刪除了,或者從其他工程考過(guò)來(lái)一個(gè)文件沒(méi)有這行的,編譯時(shí)一律是
fatal error C1010: unexpected end of file while looking for precompiled header directive,
這時(shí)你只要給該文件開(kāi)頭加個(gè)#include "stdafx.h"就了事。
這兩個(gè)文件除了這點(diǎn)之外還有一個(gè)問(wèn)題,就是這兩個(gè)文件不能從工程里刪
除,不然你編譯任何一個(gè).cpp/.c的文件都會(huì)報(bào)
fatal error C1083: Cannot open precompiled header file: 'Debug/xxx.pch': No such file or directory,
你build也不行,vc會(huì)給每一個(gè)源文件報(bào)這樣一個(gè)錯(cuò)誤。
你再把這兩個(gè)文件加入工程,一樣不行,不論你把他們加在哪,怎么編譯就是fatal error C1083,找不到.pch文件。只有新建一個(gè)工程,然后把原來(lái)的源文件都加過(guò)去,要是工程里有很多源文件,進(jìn)行了很多設(shè)置的話,可就麻煩了。
于
是,我新建了一個(gè)工程就叫test把,看看怎么會(huì)出現(xiàn)fatal error C1083的。在新建一個(gè)工程后,vc在工程目錄里生成了幾個(gè)文件,包括
test.dsp(工程文件)、StdAfx.h、StdAfx.cpp。和主程序源文件test.cpp等。如果你build工程,會(huì)在相應(yīng)的編譯目錄
下(一般為debug或release)產(chǎn)生test.pch、vc60.idb、vc60.pdb、StdAfx.obj和源程序的obj、pdb文件
和一個(gè)test.exe程序,fatal error C1083就是說(shuō)沒(méi)有這個(gè).pch文件,他也不能產(chǎn)生這個(gè)文件。那到底vc是怎么產(chǎn)生這個(gè)文件的,
又是作了什么改動(dòng)使他不能再產(chǎn)生這個(gè)文件的呢?我們將debug目錄刪除,然后編譯test.cpp文件,結(jié)果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)
看
來(lái)是編譯StdAfx.cpp產(chǎn)生了test.pch,然后再編譯test.cpp的把;再把debug目錄刪了,編譯StdAfx.cpp就ok,看看
debug目錄:test.pch、StdAfx.obj、vc60.idb、vc60.pdb。原來(lái)是編譯StdAfx.cpp產(chǎn)生的pch文件,那從
工程里刪除這個(gè)文件然后在添加這個(gè)文件為什么不行呢?肯定是在在刪除/添加StdAfx.cpp文件過(guò)程中,工程文件不一樣了。先關(guān)閉工程,把
test.dsp備份,然后打開(kāi)工程,刪除StdAfx.cpp文件,在添加,編譯,錯(cuò)誤C1083,然后關(guān)閉工程(保存工程)。fc比較一下剛才備份的
dsp和現(xiàn)在的工程文件,發(fā)現(xiàn)原來(lái)正常的那個(gè)工程文件里在SOURCE=.\StdAfx.cpp之后多這么一
行# ADD CPP /Yc"stdafx.h",而刪了StdAfx.cpp再添加的工程就沒(méi)有,其他的就完全一樣。
正常的dsp文件包含StdAfx.cpp的這兩行是這樣的:
SOURCE=.\StdAfx.cpp
# ADD CPP /Yc"stdafx.h"
# End Source File
后記:
vc
真是奇怪呀,這行# ADD CPP /Yc"stdafx.h"在新建工程時(shí)有,從工程里刪除了StdAfx.cpp就沒(méi)有了,再在工程里加上這個(gè)
StdAfx.cpp就沒(méi)有加上。看似刪除再添加好像沒(méi)有動(dòng)工程,誰(shuí)知道他來(lái)這么一手,艾,要是懂編譯懂vc的還好,要是遇上我這樣的菜鳥(niǎo),艾,只有和他
奮斗半天才把他搞得稀里糊涂。把他寫(xiě)出來(lái),免得和我一樣的菜鳥(niǎo)再費(fèi)半天勁。
其實(shí)這個(gè)pch文件是預(yù)編譯頭
(PreCompiled Header),下邊是msdn里關(guān)于PreCompiled Header和/Yc選項(xiàng)的解釋。一直都是用集成編譯環(huán)境的,
先是tc,然后是vc,沒(méi)用過(guò)make,沒(méi)寫(xiě)過(guò)makefile,對(duì)編譯、連接這些東西還不是很懂,高手見(jiàn)笑了。這個(gè)選項(xiàng)是可以在工程的設(shè)置里設(shè)的,具體
可以看下邊的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