Precompiled header
From Wikipedia, the free encyclopedia
In computer programming, a Precompiled header is a performance feature found in some C or C++ compilers used to reduce compilation time.
Contents |
[edit] Overview
In order to execute a program written in a language such as C, the source code must first be converted from readable plain text into a binary file that can be run by the computer's processor. This process is called compilation and can be a lengthy one. Commonly, when programming complex applications, use is made of header files, which are sections of code included in many other programs for performing common tasks. In order to speed up compilation, these seldom changed files can be converted into an intermediate form, which is easier for the compiler to understand (typically a dump of the parse tree) so that subsequent compilations are faster. This intermediate form is called precompiled header files, for which .PCH or .GCH extensions are typical though they may vary according to the compiler.
While this kind of header can be advantageous, there is a chance for slowing down compilation if too much unused code is contained into the headers. This can be overcome by using a larger number of smaller headers and not including those for functions that are unused. One can further break lots of dependencies by using forward declarations and Pimpl idiom.
The results of subdividing the source code are often far better[citation needed] than one could possibly achieve with precompiled headers. However, the compiler can not automatically handle its reorganization.
[edit] Usage
[edit] Example
Given a C++ file source.cpp that includes header.hpp:
// header.hpp ...
// source.cpp #include "header.hpp" ...
When compiling source.cpp for the first time with the precompiled header feature turned on, the compiler will generate a precompiled header, header.pch
. The next time, the compiler can skip the compilation phase relating to header.hpp
and instead use header.pch
directly.
[edit] stdafx.h
stdafx.h is commonly used in Microsoft Windows projects as the name of a file that describes both standard system and project specific include files that are used frequently but hardly ever changed. Since many C/C++ source files for Windows programs include stdafx.h, making small changes in it means the entire project needs to be recompiled. Consequently, few errors in stdafx.h can cause a lot of errors in the rest of the project.
Compatible compilers (for example, Visual C++ 6.0 and newer) will precompile 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; 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). This type of file can also have alternate names, including "StdInclude.h".
[edit] GCC
Precompiled headers are supported in GCC (3.4 and newer). GCC's approach is similar to these of VC and compatible compilers, despite of header files' not having pseudo-standard names. GCC can precompile any header, and it saves the precompiled version using a ".gch" suffix. When compiling source files, the compiler checks whether those files are present. If possible, the precompiled version is used. If this is not possible, then, the normal header is still present as a fall-back plan.
GCC can only use the precompiled version if the same compiler switches are set as when the header was compiled and it may use at most one. Futher, only preprocessor instructions may be placed before the precompiled header (because it must be directly or indirectly included through another normal header, before any compilable code).
GCC automatically identifies most header files using their extension. However, if this fails (e.g. because of non standard header extensions), the -x switch can be used to be sure that GCC treats the file as a header instead of a normal source file.