Click here to Skip to main content
14,333,606 members

How to Optimize Compilation Times with Precompiled Headers (PCH Files)

Rate this:
4.89 (6 votes)
Please Sign up or sign in to vote.
4.89 (6 votes)
25 May 2017CPOL
Are you curious what exactly precompiled headers, or .pch files are and why you would use them? This article will cover this and provide an example of how to use them.

This article is a sponsored article. Articles such as these are intended to provide you with information on products and services that we consider useful and of value to developers

Click here to download IncrediBuild for free and accelerate your C++ development.

What Are Header Files?

If you are asking this question then you are most likely new to C and C++ (and honestly, a quick refresher never hurt anyone.)

Header files (.h extension) are used in C and C++ to define the structure of functions, templates, and macros that can be included across one or more source files. The idea was introduced in the 1970s, when loading just the header file was a way to save precious memory and other system resources.

Of course, we’ve come a long way since the 1970s, and memory may no longer be so rare a commodity on most systems. But it’s always good to be as lean and efficient as possible, and header files play an important role in achieving that goal.

The world’s most well-known header file, which is at the top of every "hello world" sample app written for C, is stdio.h, which includes the structure of the actual printf function that writes text out to the console.

$ cat hello-world.c
#include <stdio.h>
int main() {
   printf("Hello World\n");
   return 0;

What Are Precompiled Headers?

A precompiled header has exactly what the name suggests. During the compilation process for a C/C++ application, the step to compile the header files is already completed. Having the header files already compiled can reduce compile times significantly.

How Do Precompiled Headers Work?

Precompiled headers work by preserving the headers that were compiled during the last run. The very first time you compile your project, and you are configured to support precompiled headers, the compiler and supporting tools (like nmake or ant) will be compiling the headers and making PCH files.

The second and subsequent time the compiler runs it will compare the header files to what was compiled last time, and if there was no change it will use the precompiled header. This is why structuring your headers properly ahead of time, and having a somewhat stable code base make this performance enhancement method really shine.

Enabling Precompiled Headers on Popular Compilers

If you are using Visual Studio and have an interest in enabling Precompiled Headers, please read the MSDN documentation on the subject.

GCC also has documentation on enabling precompiled headers.

If you prefer Intel’s compiler, it also fully supports precompiled headers.

Example of the Difference in Time That Using Them Can Provide

Let’s create a very simple program.

$ cat header.h 
#include <CoreFoundation/CoreFoundation.h>
#include <objc/objc.h>
#include <objc/objc-runtime.h>
#include <iostream>

$ cat simple.cpp 
#include "header.h"
int main() {
  return 0;

And time a compile:

$ time g++ -std=c++11 simple.cpp -o simple

real	0m1.152s
user	0m0.424s
sys	0m0.585s

Now let’s precompile the header and then recompile for time:

$ g++ -std=c++11 header.h
$ time g++ -std=c++11 simple.cpp -o simple

real	0m0.601s
user	0m0.411s
sys	0m0.149s

Notice the difference? It saved half a second. Now imagine the difference at scale.

Conclusion and Additional Compile Time Optimizations

Precompiled headers are a great first step for any code base to reduce the time it takes to compile the application—the biggest single caveat being that the application code base needs to be structured to really take advantage of it. Enabling precompiled headers isn’t a silver bullet, but it will often help a little—and with some effort or pre-planning, can help a lot. (There are arguments for and against precompiled headers if you want some additional background.)

If you want to further reduce compile times, or have an especially complicated project precompiled headers weren’t considered at the start, like an Xbox game, then there are additional steps you can take. The single biggest of these steps is to massively parallelize your compile process. Going from the four cores on your desktop to 400 or more cores on a server farm can’t hurt, right? The trick is having the server capacity available to you, and having processes and tools in place to manage the build so that it all still gets done in the correct order, and is assembled properly at the end.

This is where companies like IncrediBuild come into play. They offer services that start with a plugin that can integrate into the IDE, supporting precompiled headers, assisting with test automation, and provide a cloud service that has the capacity you need to make your compiles more time-efficient. They have seen reductions in compile time of up to 90%.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Authors

Vince Power
United States United States
Vince Power is a Solution Architect who has a focus on cloud adoption and technology implementations using open source-based technologies. He has extensive experience with core computing and networking (IaaS), identity and access management (IAM), application platforms (PaaS), and continuous delivery.

United States United States
IncrediBuild Software Ltd, the developer of IncrediBuild, is a leading solution provider of software acceleration technology. By harnessing unutilized processing power in private and public cloud environments, IncrediBuild accelerates Windows-based computational software with easy deployment and migration. With its unique process virtualization technology, IncrediBuild has become the de facto standard solution for code-build acceleration. More than 120,000 users at over 2,000 companies and organizations depend on IncrediBuild application and build acceleration, including 20 Fortune 100 companies.

Comments and Discussions

QuestionNot my cup of tea Pin
YvesDaoust26-May-17 2:31
memberYvesDaoust26-May-17 2:31 
AnswerRe: Not my cup of tea Pin
Erik Burd29-May-17 10:43
professionalErik Burd29-May-17 10:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 25 May 2017

Tagged as


2 bookmarked