views:

576

answers:

3

I'm wondering what others have experienced implementing cross-platform (linux and windows) precompiled headers in C++. I'm thinking of what Visual Studio lets you do with stdafx.h files that can drastically improve compile times for large amounts of C++ code by precompiling the common headers used across the board (std/boost/etc headers). Is there a way to make this happen cross platform with some kind of framework or something?

What kind of experience have you had trying to do this?

Edit

I don't really mean sharing the actual resulting pch, I'm more interested in frameworks that can generate pch and whatever the equivelant would be for, say, gcc, when compiled on that specific platform.

A: 

If you mean porting the precompiled header database (.pch or whatever) between platforms, then this is not possible as the headers will have different contents on the different platforms.

anon
+2  A: 

The visual studio precompiled headers are based on a header file including all that should be precompiled, typically commonly included rarely changed header files such as standard library stuff. It's connected to a stdafx.cpp which is set to "generate precompiled header" in the settings, it only includes stdafx.h.

Visual studio then forces all files to include stdafx.h as its first preprocessor definition to avoid problems with headers included before it or changed #define macros that affects the parsing of stdafx.h.

I think the easiest way of mapping this behaviour to g++ is to make it precompile only stdafx.h and include other headers normally. It will be similar to what you do in visual c++. You can also rename stdafx to something less stupid like "precompiled_.h or something. It's easy to setup visual studio to use this file instead.

I have implemented this kind of system with using make files for g++ and it gave some performance, but I didn't manage to get the same kind of performance boost as I get from precompiled headers in visual studio. This was some time ago and g++ might have improved since then. I've managed to get CMake to generate visual studio projects with precompiled headers, I haven't tried it for their Makefile generation yet but it should be no problem.

Visual Studio has some other tricks to improve compilation speed. One is compiling many cpp-files with the same settings in one batch. This could be done manually using what's usually called a unity build system where you include multiple cpp-files into one file and build it in one go, saving you header parsing and disk io.

Laserallan
Realized I was a bit off topic after posting this answer, but I keep it anyway, someone might find it useful :)
Laserallan
+3  A: 

gcc will automatically precompile headers if you pass a header file instead of an implementation file. Simply add the -x switch if it tries to produce object code. GCC will always look for the .gch file before the header, so it's simple enough to use. This page has more info. Add it to your Makefile to automate the process.

greyfade