7

When using precompiled headers, does it make a difference (or has a bad effect) when I include a header, which I already have included in the precompiled header, also in a regular header?

Or in other words: when I include a header x in another header file and at the same time include x in my precompiled header file, does this prevent the optimization provided by precompiled header to kick in?

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181

2 Answers2

13

No

(In general). What's going to happen is that, during compilation, if you're using precompiled headers, and the compiler spots a header that is already present in the precompiled form, it will opt to use the precompiled form.

In fact, it's good practice to continue using your includes as if you never had precompiled headers on in the first place. This helps in case you turn off precompiled headers in the future or modify the list of headers in it, or someone else decides to do their own out-of-source build that doesn't use PCH.

Community
  • 1
  • 1
AndyG
  • 39,700
  • 8
  • 109
  • 143
  • 1
    Very interesting and I agree with your reasoning. I'd just like to have a reference or something that could prove your answer. Can you provide one? – Mike Lischke Jan 12 '17 at 13:51
  • 1
    Microsoft' documentation is pretty scant on how exactly theirs works, but they do include a [code example](https://msdn.microsoft.com/en-us/library/de9s74bh.aspx) where they continue to include the header that got precompiled. For [gcc](https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html) they explicitly state that "A precompiled header file is searched for when #include is seen in the compilation. As it searches for the included file (see Search Path) the compiler looks for a precompiled header in each directory just before it looks for the include file in that directory." – AndyG Jan 12 '17 at 14:40
  • https://stackoverflow.com/q/57179537/3427520 @AndyG Here is a related question, if you are interested. – zwcloud Jul 24 '19 at 09:26
  • @zwcloud Question was removed. I'm curious, what was it about? In the future, consider sharing the title of the question with a link, as opposed to just the link ;) – Marc.2377 Sep 11 '19 at 04:18
  • @Marc.2377 It was deleted by Community♦ Aug 24 at 0:00 (RemoveDeadQuestions) The question is "Will the compile time be reduced after removing extra includes from cpp if they have been included in pch?" – zwcloud Sep 11 '19 at 05:21
  • 1
    @zwcloud the answer to "Will PCH improve compile times?" is almost always yes, even for full rebuilds, although the real win is typically in incremental builds. There's no guarantee, however. PCH is as much of an art form as anything – AndyG Sep 11 '19 at 16:50
  • 1
    (cont'd) As for whether times will be reduced after removing duplicate includes, it's hard to say. Probably not for small projects. Maybe for bigger projects. However, PCH causes a bigger memory footprint which may hurt times due to less RAM available. Best to test and find out. – AndyG Sep 11 '19 at 18:25
1

Precompiled headers are a compile-time optimization. The code must be written as if there was no precompiled headers. The support for those headers can be added entirely using the arguments to the compiler, i.e. by tweaking the build process and adding a "catch-all" header if the build system can't generate one for you (it really should, these days!).

TL;DR: Precompiled headers are a switch you throw in your build script. Nothing more. They should not leak outside of the build scripts.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313