precompiled headers
They do not only exist for Microsoft CL, gcc has them as well.
Precompiling a header file is very much like compiling .c into .o You compile 1 .h file into 1 .h.pch file, under the hood that file can include x other header files, but the top is 1-1.
This was an attempt to get c++ compiling faster but turns out not te be the wonder cure. I got a speed improvement of around 20 % So for now, it is not really worth the effort. Still here are my findings.
tutorial
This example file :
It is small but you need libboost-dev installed to get it compiled. This is also part of the example since boost has a LOT of headers and is a perfect example for speed gain. If you compile it it takes about ten seconds:
| compile slow | |
|---|---|
10 seconds is a very long wait in my opinion. So
Now let's see what headers are used in this compilation, g++ has the -H flag for that:
| list used headers | |
|---|---|
The list you get seems endless,so there must be some time to be saved there. Let's precompile the header file included int test.cpp
sudo is needed, because it will try to create /usr/include/boost/xpressive/xpressive.hpp.gch on that location. After this, if you run the example again with -H you will see :
| list headers | |
|---|---|
The -H now lists the precompiled header instead, the ! means it was able to use it correctly, if not it will be an 'x'.
Timing the compilation again makes it 8 seconds. Not the immense improvement i hoped for, but still. ## scons
g++ is used, because let's face it, gcc is fast enough, but that also would work.G++ just looks for .h.gch first, and otherwise takes .h ! But beware !!, if you change the original header. You must either delete the gch file or recompile it !!
A simple BUILDER to generate the precompiled headers is implemented in the scons script for klopt web services, the major parts are :
Ok this is a 3 stage rocket, maybe it can be done shorter but i remain a non-scons expert.
- create the command line you want to use as a python string
- create an scons Action from that string
- create a scons Builder using that action