Jump to content

Precompiled header

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by K3rb (talk | contribs) at 00:43, 27 November 2005 (Initial revision). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Precompiled header is a performance feature found in some C or C++ compilers used to reduce compilation time. The idea is to compile seldomly changed header files into object code so that subsequent compilations are faster, at the cost of the initial, slower precompilation. Precompiled header files typically carry the .PCH file extension.

For example, suppose we have two files:

// header.hpp
...
// source.cpp
#include "header.hpp"
...

Now, if the precompiled header feature is turned on when compiling source.cpp for the first time, the compiler will generate header.pch. The next time source.cpp is compiled, the compiler can skip compiling header.hpp and more quickly use header.pch directly, instead, in the construction of source.cpp's output.

The advantages of precompiled headers become more pronounced as the number of header files increases or as the amount of code in the header files become significantly large. This is the case with newer C++ code, such as the Boost library, which implements almost all of its code inside header files due to template constraints.

Precompiled headers are suppored in Visual C++ (6.0 and newer) and GCC (3.4 and newer).