C/C++ projects can benefit from using precompiled headers to improve compile time. GCC added support for precompiled headers in 2003 (version 3.4), and the current documentation can be found at https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html.
Even with the emergence of C++ modules, precompiled headers remain relevant for several reasons:
- Precompiled headers share implementation aspects with modules (e.g., AST serialization in Clang).
- Many C++ projects rely on the traditional compilation model and are not converted to C++ modules.
- Modules may possibly use some preamble-like technology to accelerate IDE-centric operations.
- C doesn't have C++ modules.
This article focuses on Clang precompiled headers (PCH). Let's begin with an example.