Author Topic: Precompiled Headers Strategy  (Read 4770 times)

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Precompiled Headers Strategy
« on: November 02, 2005, 06:03:56 pm »
Why was this "Precompiled Headers Strategy" introduced and
why is the default mode "PCH in a directory alongside original header" ?

i always thought, that gcc does find the *.h.gch only in the same directory where the *.h file resides  :?
how can you tell gcc where to look for the PCH file *.h.gch ?

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: Precompiled Headers Strategy
« Reply #1 on: November 02, 2005, 06:59:11 pm »
First of all, the "default" is subject to change.

Now, what each one does:

  • Gen. PCH in a dir alongside orig. header:

This creates a directory <orig_filename>.h.gch at the dir where the original header resides. When gcc looks for PCH it recognizes if it sees a directory with the name the PCH would have. It looks in that directory for any PCH (doesn't matter how it's named) and uses the appropriate one based on current build settings.
This works even with the "-include" gcc option to include a PCH in every source file without having to edit it (that's why it's set as default, although I 'd like the next as default).
  • Gen. PCH in object output dir

This, probably the most common, creates the PCH in the object output dir. When in this mode, C::B automatically adds as first include search dir the dir with the PCH. So, all you have to do is #include your PCH in every source file of your project.
This doesn't work with the "-include" command line option, if the original header is in the CWD (i.e same folder with project file - like all projects from template). The original header takes precedence in that case.
  • Gen. PCH alongside orig. header

The most bulletproof solution as long as you 're not having different configurations (e.g. debug/release). If you have diff. configurations, this mode is not good for you...
[/list]

Well, that's about it :)
Be patient!
This bug will be fixed soon...

Offline Urxae

  • Regular
  • ***
  • Posts: 376
Re: Precompiled Headers Strategy
« Reply #2 on: November 02, 2005, 07:04:52 pm »
Note: I started typing this before Yiannis posted, but I'll just post anyway.

Why was this "Precompiled Headers Strategy" introduced and
why is the default mode "PCH in a directory alongside original header" ?

If you put it in a directory instead of directly next to the header, you can put multiple files in there. This comes in handy for files that are precompiled in different targets (with different compiler options). For instance, in Code::Blocks itself that might just make sdk/sdk_precomp.h unnecesary: you could add sdk/sdk.h to the 'sdk' target as well, and the precompiled headers would be sdk/sdk.h.gch/src and sdk/sdk.h.gch/sdk. (EDIT: I just ran a diff and sdk.h doesn't #include <uservarmanager.h>, the latest addition, so that would have to be added)

Quote
i always thought, that gcc does find the *.h.gch only in the same directory where the *.h file resides  :?
how can you tell gcc where to look for the PCH file *.h.gch ?

Actually, GCC looks for a gch file or directory in each include directory just before looking for any #included header. So any header #included only like <this> (never like "this") you can put the .gch file/dir in any directory in the include path, as long as it's searched before the one the actual header is in (or in the same one, obviously).
The reason you can't do this with headers #included like "this" is that in that case the current directory is searched first, so if the header is there, then so should the .gch be.
Before Code::Blocks supported precompiled headers, I actually had an extra directory I put in the path that only contained wx/precomp.h.gch and wx/setup.h (which #defined WX_PRECOMP and then used #include_next to get the actual setup.h).
« Last Edit: November 02, 2005, 07:17:38 pm by Urxae »

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Re: Precompiled Headers Strategy
« Reply #3 on: November 02, 2005, 07:30:19 pm »
thanks to Yiannis and Urxae for all of your explanations, i understand better now !