Disclaimer: ChatGPT generated document.
Precompiled headers (PCH) are a compiler optimization technique that speeds up compilation by precompiling common header files into a binary format that can be reused across multiple source files.
- Reduces compilation time (especially in large projects).
- Avoids redundant parsing of large headers.
- Improves incremental builds (only recompile when necessary).
- Preprocessing: Expands macros, includes headers.
- Parsing: Converts C++ code into an abstract syntax tree (AST).
- Semantic Analysis: Checks types, overloads, etc.
- Code Generation & Optimization.
- Assembly & Linking.
Each source file (.cpp) separately includes standard and project headers, causing the same headers to be parsed and compiled repeatedly.
Example:
// file1.cpp
#include <iostream>
#include <vector>
#include <string>
// file2.cpp
#include <iostream>
#include <vector>
#include <string>Without PCH, the compiler processes <iostream>, <vector>, and <string> for every single source file!
- A special header file (
.hor.hpp) is created containing commonly used headers. - The compiler precompiles this file into a binary (
.pch,.gch, or.pchi). - Each
.cppfile includes this precompiled header instead of re-parsing the individual headers.
// pch.h (Header File for Precompiled Headers)
#ifndef PCH_H
#define PCH_H
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#endif // PCH_HUsing GCC/Clang:
g++ -x c++-header pch.h -o pch.h.gchUsing MSVC (Windows)
cl /Yc pch.h /Fp:pch.pch// file1.cpp
#include "pch.h" // Includes precompiled header
void func1() { std::cout << "Hello from file1\n"; }
// file2.cpp
#include "pch.h" // Includes precompiled header
void func2() { std::cout << "Hello from file2\n"; }GCC:
g++ -include pch.h file1.cpp file2.cpp -o programMSVC:
cl /Yu pch.h file1.cpp file2.cpp /Fp:pch.pchβ
The compiler skips parsing the headers inside pch.h for every .cpp file, drastically reducing compile time!
- Reduces redundant header parsing.
- Greatly speeds up incremental builds.
- Ideal for projects with many files sharing common headers.
- Since the compiler doesnβt have to keep reprocessing the same headers.
- Requires manual setup (
pch.h, compiler flags). - Not needed for small projects.
- Modifying
pch.hforces full recompilation. - Dependencies must be carefully managed.
- Different compilers handle PCH differently.
- GCC, Clang, and MSVC have different implementations.
- Good Candidates:
<iostream>,<vector>,<map>,<algorithm>- Third-party stable libraries (Boost, Eigen).
- Bad Candidates:
- Frequently modified project-specific headers.
#ifndef PCH_H
#define PCH_H
// Includes go here
#endif- For small projects, PCH setup overhead may outweigh benefits.
-
Uses
.gchfiles (pch.h.gch). -
Compile PCH:
g++ -x c++-header pch.h -o pch.h.gch
-
Use PCH:
g++ -include pch.h main.cpp -o program
-
Uses
.pchfiles. -
Compile PCH:
clang++ -x c++-header pch.h -o pch.pch
-
Use PCH:
clang++ -include pch main.cpp -o program
-
Uses
.pchfiles. -
Compile PCH:
cl /Yc pch.h /Fp:pch.pch
-
Use PCH:
cl /Yu pch.h file1.cpp file2.cpp /Fp:pch.pch
Instead of multiple .cpp files, merge all into one file to reduce header parsing overhead.
// unity.cpp (Combines multiple files)
#include "file1.cpp"
#include "file2.cpp"Compile:
g++ unity.cpp -o programβ Faster than PCH in some cases.
Use #pragma once instead of #ifndef guards to speed up preprocessing.
#pragma once
#include <iostream>
#include <vector>For GCC/Clang, ccache caches previous compilations.
sudo apt install ccache # Install
export CC="ccache g++" # Enableβ Faster recompilation without modifying source code.
| Feature | Without PCH | With PCH |
|---|---|---|
| Header Processing | Parsed for every .cpp file |
Precompiled once |
| Compilation Speed | Slower | Faster (up to 50%+ improvement) |
| Setup Effort | None | Requires manual setup |
| Best For | Small projects | Large projects |
| Compiler Support | All compilers | Needs compiler-specific flags |
π Precompiled headers are a great tool for large C++ projects to speed up compilation! π
