C++ - Precompile header file

来源:互联网 发布:北电网络被谁收购 编辑:程序博客网 时间:2024/05/22 18:04

缘起

In the C and C++ programming languages, aheader file is afile whose text may be automatically included in anothersource file by theC preprocessor, usually specified by the use ofcompiler directives in the source file.

Header files can sometimes contain a very large amount of source code (for instance, the header fileswindows.h and Cocoa/Cocoa.h onMicrosoft 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 aprecompiled 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 specificinclude 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 theMicrosoft Foundation Classes (MFC). Optionally, Visual Studio projects may avoid pre-compiled headers, as well as they may specify an alternative name (thoughstdafx.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

原创粉丝点击