GCC and Precompiled Headers
Asked Answered
H

1

6

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 #defines 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 #defineing 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 #included by the current library was modified (or it #includes 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.

Hadden answered 15/9, 2012 at 13:53 Comment(0)
S
15

Current GCC (i.e. 4.7) and previous versions of it works nicely with precompiled headers only when you have a single common header to your application, and when that single header (which includes in turn all the system ones, and the library specific ones, required by the application) is #include-d (as the first non-comment lexeme of your sources) by every source of your application.

So you should have a single yourapp.h and have every source file (i.e. every compilation unit) of yourapp starting with #include "yourapp.h" with the same preprocessing options (i.e. -D or -I or -U) on the command line. That youapp.h header file is usually #include-ing many others, e.g. system headers (or GTK or Qt ones) like <stdlib.h> or <sys/poll.h> or [in C++] <algorithm> or <gtk/gtk.h> or <QtGui> etc.

Recall that -H is a useful option to get gcc tell you what is included.

Your source files might have some additional #include after #include "yourapp.h" if so wanted.

After a [single] precompiled header has been included by GCC, you may of course #define macros, #include some non-precompiled header, do conditional compilation with #ifdef, etc. But that preprocessing won't be "pre-compiled"!

This may not fit your needs or habits.

Some people (notably from Google, notably Diego Novillo) are working on the PreParsed Header (pph) branch to improve the situation, but the current GCC trunk has not got that work yet.

The explanation about that behavior of GCC is that a preprocessed header is essentially a persistent serialized checkpoint of the entire GCC heap (related to memory management inside GCC thru Ggc and GTY and gengtype). That checkpointed heap can be loaded only when gcc is in its initial empty state. As soon as something is known to gcc (actually cc1 or cc1plus) it cannot load any more any precompiled header file *.h.gch and will revert to parsing the textual header file *.h.


addenda (november 2014 and later)

Even GCC 4.9 wants a single precompiled header. The pre-parsed header effort by Diego Novillo et al. has been abandoned.

Future versions (post C++14) of the C++ standard might define a module mechanism. See e.g. n4047 proposal and the C++20 standard.

(additional addenda, summer 2020) This still holds for GCC-10 where several static analyzer options exist. See also the Clang static analyzer and this draft report. Consider using Frama-C.

Stradivarius answered 15/9, 2012 at 14:4 Comment(8)
Yeah, having a single header to include everything eliminates most of the complications, but I've seen a really good example of how this can mess things up at my previous work place. Thanks for the nice and well documented answer!Hadden
Interesting. Borland introduced the notion of precompiled headers, and did exactly what PreParsed Headers are now trying to do. Then Microsoft came along and announced that their approach was better, and everyone followed along.Quarterhour
@Basile Regarding your last edit: Yes, sources might have additional #includes, but they can't be also part of the pre-compiled header, right?Hadden
The single pre-compiled header may (and often should) of course contain itself many #include-sStradivarius
@Mihai: What do you mean by a source being part of the pre-compiled header?Stradivarius
@Basile Sorry for the confusing comment. What I meant was that the additional headers #included in a .cpp file can't be part of the pre-compiled header at this time, since it is generated just for a single global (big) header, right?Hadden
Only one single yourapp.h file can be a pre-processed header, and its yourapp.h.gch preprocessed form is used only when the first lexemes gcc encounters is #include "yourapp.h"Stradivarius
It extremely speeds up development on windows (x 10 in my case) but doing some testing it might be insignificant or even worse on linux (tried LLVM compiler not GCC). I was really dissappointed by the result a few years ago. Reason is IMHO that MSVC does not start a process for every compilation unit and has to reload the headers but you specify multiple c files in the command line. Then for 100 c files the precompiled header is verified and deserialized exactly once.Brazil

© 2022 - 2024 — McMap. All rights reserved.