Precompiled headers is a mechanism to speed up compilation by creating a partially processed version of some header files, and then using that version during compilations rather then repeatedly parsing the original headers. Boost.Build supports precompiled headers with gcc and msvc toolsets.
To use precompiled headers, follow the following steps:
Create a header that includes headers used by your project that you want precompiled. It is better to include only headers that are sufficiently stable — like headers from the compiler and external libraries. Please wrap the header in
#ifdef BOOST_BUILD_PCH_ENABLED, so that the potentially expensive inclusion of headers is not done when PCH is not enabled. Include the new header at the top of your source files.
Declare a new Boost.Build target for the precompiled header and add that precompiled header to the sources of the target whose compilation you want to speed up:
cpp-pch pch : pch.hpp ; exe main : main.cpp pch ;
You can use the
c-pchrule if you want to use the precompiled header in C programs.
pch example in Boost.Build distribution can be
used as reference.
Please note the following:
The inclusion of the precompiled header must be the first thing in a source file, before any code or preprocessor directives.
The build properties used to compile the source files and the precompiled header must be the same. Consider using project requirements to assure this.
Precompiled headers must be used purely as a way to improve compilation time, not to save the number of
#includestatements. If a source file needs to include some header, explicitly include it in the source file, even if the same header is included from the precompiled header. This makes sure that your project will build even if precompiled headers are not supported.
On the gcc compiler, the name of the header being precompiled must be equal to the name of the
cpp-pchtarget. This is a gcc requirement.
Prior to version 4.2, the gcc compiler did not allow anonymous namespaces in precompiled headers, which limits their utility. See the bug report for details.