缘起
In the C and C++ programming languages, a header file is a file whose text may be automatically included in another source file by the C preprocessor, usually specified by the use of compiler directives in the source file.
Header files can sometimes contain a very large amount of source code (for instance, the header files windows.h
and Cocoa/Cocoa.h
on Microsoft Windows and Mac OS X, respectively).
To reduce compilation times some compilers allow header files to be precompiled into a form that is faster for the compiler to process. This intermediate form is known as a precompiled header, and is commonly held in a file named with the extensions .pch or .gch.
Implementation in Windows - stdafx.h
stdafx.h is a file, generated by Microsoft Visual Studio IDE wizards, that describes both standard system and project specific include files that are used frequently but hardly ever change.
Compatible compilers (for example, Visual C++ 6.0 and newer) will pre-compile this file to reduce overall compile times. Visual C++ will not compile anything before the #include "stdafx.h"
in the source file, unless the compile option /Yu'stdafx.h'
is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
The AFX in stdafx.h stands for Application Framework eXtensions. AFX was the original abbreviation for the Microsoft Foundation Classes (MFC). Optionally, Visual Studio projects may avoid pre-compiled headers, as well as they may specify an alternative name (though stdafx.h is used by default).
A common problem relevent
When compiling with Microsoft Visual C++, you get the following error: “c:\vcprojects\test.cpp(263) :fatal error C1010: unexpected end of file while looking for precompiled header directive”
Answer : This error occurs when the Microsoft Visual C++ compiler is set to use precompiled headers but one of your C++ code files does not include the stdafx header as the first line. To fix this problem, simply locate the file producing the error (in the above error, test.cpp is the culprit), and add the following line at the very top of the file:
1 | #include "stdafx.h" |
Alternatively, you can turn off precompiled headers.
参考:
PCH Files in the Build Process: http://msdn.microsoft.com/en-us/library/hd8sctab%28v=vs.80%29.aspx