I'm currently speeding up compilation of a large C++ project (there is some C code too). Initially I'm
- removing unnecessary system includes; and
- introducing precompiled headers for common system includes such as
stddef.h
orvector
. But not for includes likestdio.h
oriostream
which shouldn't be commonly used.
I'd thought that system headers would have include guards and thus would be covered by gcc's multiple include optimization. However it seems that not all headers follow this guideline. For example stdlib.h
has #ifndef STDLIB_H
at the start, and stddef.h
follows the same pattern. But assert.h
doesn't have a standard include guard, and nor does cstddef
or cstdlib
.
I've been using the -H
option in gcc to track and subsequently analyse include dependencies in this project. What I've observed is that for a small sub-project with only 6 files, stdlib.h
and stddef.h
, which follow the include guards pattern, show up 6 times in the output, whereas the files that don't can show up 10s or 100s of times. I'm a little concerned that as some of these files may be on a remote network drive, compilation may be slower.
When I initially got precompiled headers working in the project, only including C++ headers such as vector
, and headers from some internal libraries, I was a little surprised to only get a 20% performance increase. When I've done similar in Visual C++ I've seen greater increases. (Possibly related GCC build time doesn't benefit much from precompiled headers.)
I'm relatively new to gcc, so I may have missed something. My questions are:
- Why do some headers have standard include guards, and some not?
- Should I be concerned about the effect on compilation speed?
- If so, how to address? I wouldn't mind adding
assert
to the precompiled headers, but I wouldn't want to addcstdlib
, for example.