I've tried using precompiled headers with GCC to speed up one of my builds. It is a project with around 80 files, most of which include a single header, which I precompiled. This header includes a large number of other headers. At least on paper this seems like a good usecase for using PCH.
When using clang, I get the expected speedup, which is around 2x faster. On gcc however, compilation takes significantly longer.
I store my PCHs in a separate directory, and have tried both adding the directory with -I
, as well as specifying the PCH directly with -include
(that didn't work: gcc ended up including both PCH and normal header). PCHs are compiled with the same options as source files.
Without PCH compilation takes around 13 seconds. With PCH it takes around 18 seconds. I validated with -H
and -Winvalid-pch
that gcc (only) includes the precompiled header. The complete output of -H
for a single file looks like this:
! .bake_cache/x64-Linux-debug/include/corto.h.gch
./src/lang/boolean.c
The !
should indicate that gcc successfully loaded the pch:
-H Print the name of each header file used, in addition to other normal activities. Each name is indented to show how deep in the ‘#include’ stack it is. Precompiled header files are also printed, even if they are found to be invalid; an invalid precompiled header file is printed with ‘...x’ and a valid one with ‘...!’ .
From: https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html
I'm at a loss. gcc seems to be able to locate and use the PCH, but still takes longer. Any idea what may be causing this?