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%.