Skip to content

Instantly share code, notes, and snippets.

@MangaD
Created March 15, 2025 08:06
Show Gist options
  • Select an option

  • Save MangaD/fde1290be3e8b362ed524117382e9a47 to your computer and use it in GitHub Desktop.

Select an option

Save MangaD/fde1290be3e8b362ed524117382e9a47 to your computer and use it in GitHub Desktop.
Comprehensive Guide to Precompiled Headers (PCH) in C++

Comprehensive Guide to Precompiled Headers (PCH) in C++

CC0

Disclaimer: ChatGPT generated document.

1. Introduction to Precompiled Headers

πŸ”Ή What Are Precompiled Headers?

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.

πŸ”Ή Why Use Precompiled Headers?

  • Reduces compilation time (especially in large projects).
  • Avoids redundant parsing of large headers.
  • Improves incremental builds (only recompile when necessary).

2. Why Compilation Is Slow Without PCH

πŸ”Ή What Happens During Compilation?

  1. Preprocessing: Expands macros, includes headers.
  2. Parsing: Converts C++ code into an abstract syntax tree (AST).
  3. Semantic Analysis: Checks types, overloads, etc.
  4. Code Generation & Optimization.
  5. Assembly & Linking.

πŸ”Ή Problem: Redundant Header Processing

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!


3. How Precompiled Headers Work

πŸ”Ή Step-by-Step Process

  1. A special header file (.h or .hpp) is created containing commonly used headers.
  2. The compiler precompiles this file into a binary (.pch, .gch, or .pchi).
  3. Each .cpp file includes this precompiled header instead of re-parsing the individual headers.

πŸ”Ή Example: Creating a Precompiled Header

Step 1: Create pch.h (Precompiled Header)

// 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_H

Step 2: Compile the Precompiled Header

Using GCC/Clang:

g++ -x c++-header pch.h -o pch.h.gch

Using MSVC (Windows)

cl /Yc pch.h /Fp:pch.pch

Step 3: Use the Precompiled Header in Source Files

// 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"; }

Step 4: Compile the Source Files Using PCH

GCC:

g++ -include pch.h file1.cpp file2.cpp -o program

MSVC:

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!


4. Advantages of Precompiled Headers

βœ… Faster Compilation

  • Reduces redundant header parsing.
  • Greatly speeds up incremental builds.

βœ… Optimized Large Projects

  • Ideal for projects with many files sharing common headers.

βœ… Reduced Memory Usage

  • Since the compiler doesn’t have to keep reprocessing the same headers.

5. Disadvantages of Precompiled Headers

❌ Increased Complexity

  • Requires manual setup (pch.h, compiler flags).
  • Not needed for small projects.

❌ Fragility (Recompiling on Changes)

  • Modifying pch.h forces full recompilation.
  • Dependencies must be carefully managed.

❌ Portability Issues

  • Different compilers handle PCH differently.
  • GCC, Clang, and MSVC have different implementations.

6. Best Practices for Using Precompiled Headers

βœ… Only Include Headers That Rarely Change

  • Good Candidates:
    • <iostream>, <vector>, <map>, <algorithm>
    • Third-party stable libraries (Boost, Eigen).
  • Bad Candidates:
    • Frequently modified project-specific headers.

βœ… Use Conditional Compilation to Prevent Multiple Inclusions

#ifndef PCH_H
#define PCH_H
// Includes go here
#endif

βœ… Use PCH in Large Codebases, Not Small Projects

  • For small projects, PCH setup overhead may outweigh benefits.

7. Compiler-Specific PCH Support

πŸ”Ή GCC

  • Uses .gch files (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

πŸ”Ή Clang

  • Uses .pch files.

  • Compile PCH:

    clang++ -x c++-header pch.h -o pch.pch
  • Use PCH:

    clang++ -include pch main.cpp -o program

πŸ”Ή MSVC (Microsoft Visual Studio)

  • Uses .pch files.

  • Compile PCH:

    cl /Yc pch.h /Fp:pch.pch
  • Use PCH:

    cl /Yu pch.h file1.cpp file2.cpp /Fp:pch.pch

8. Alternative Optimization Techniques

πŸ”Ή Unity Builds (Single Translation Unit)

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.


πŸ”Ή #pragma once (Faster Header Guards)

Use #pragma once instead of #ifndef guards to speed up preprocessing.

#pragma once
#include <iostream>
#include <vector>

πŸ”Ή Using ccache to Cache Compilation

For GCC/Clang, ccache caches previous compilations.

sudo apt install ccache  # Install
export CC="ccache g++"   # Enable

βœ… Faster recompilation without modifying source code.


9. Summary Table

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! πŸš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment