After reading this nice article (The Care and Feeding of Pre-Compiled Headers), I have some doubts regarding how these can actually work in real life. More specifically, how can I know that I need to trigger the rebuild of the pre-compiled header in the following scenarios:
- I decide to
#define
something in one of my .cpp files that alters the way the pre-processor interprets some headers that are already included in my pre-compiled header - I include another header in one of my .cpp files which
#define
s a specific pre-processor directive that alters the way the pre-processor interprets a header already included in the precompiled header - Even worse, the previous issue can happen recursively, when certain headers
#include
other headers
Should the usage of pre-compiled headers enforce a certain restrictive coding style like limiting the number of headers included in .cpp files to one and never #define
ing stuff in a .cpp file?
While Microsoft's compiler probably does a decent job with pre-compiled headers (by applying some MS-specific voodoo) because, as far as I know, it provides the /Yc
and /Yu
options that are supposed to do all the plumbing, for GCC it seems that this functionality requires a lot of manual work and creativity in the Makefile and I wasn't able to find a template that is supposed to address all the pitfalls of using pre-compiled headers.
For example, if I have a project which builds several libraries, in order not to rebuild all of them after each change, I have to use some really cute sed
tricks in the Makefile to detect if one of the headers #include
d by the current library was modified (or it #include
s a modified header). I fear to even think of the complications that pre-built headers would actually imply in order for the build script to rebuild them each time it is necessary.