This question is perhaps somehow odd, but how can I speed up g++ compile time? My C++ code heavily uses boost and templates. I already moved as much as possible out of the headers files and use the -j option, but still it takes quite a while to compile (and link).
Are there any tools out there which analyse my code and point out bottle-necks for the compiler? Or can one somehow profile the compiler running on my code? This would be really nice, because sometimes I have the impression, that I spent too much time staring at the compiler console log ...
Try the PIMPL technique, this question: What techniques can be used to speed up C++ compilation times?
It'll prevent the compiler from following the chain of header files and implementations every time you need to do something.
Usually, the most expensive parts of compilation are (a) reading the source files (ALL of them) and (b) loading the compiler into memory for each source file.
If you have 52 source (.cc) files, each of which #includes 47 #include (.h) files, you are going to load the compiler 52 times, and you are going to plow through 2496 files. Depending on the density of comments in the files, you may be spending a fair chunk of time eating useless characters. (In one organization I have seen, header files varied between 66% and 90% comments, with only 10%-33% of the file being "meaningful". The single best thing that could be done to enhance readability of those files was strip out every last comment, leaving only code.)
Take a long look at how your program is physically organized. See whether you can combine source files, and simplify your hierarchy of #include files.
Decades ago, companies like IBM understood this, and would write their compilers so that the compiler could be handed a list of files to compile, not just one file, and the compiler would only be loaded once.
What has been most useful for me:
-j3
globally for make. Make sure your dependency graphs are correct in your Makefile, though, or you may have problems.-O0
if you're not testing execution speed or code size (and your computer is fast enough for you not to care much about the (probably small) performance hit).If you're doing a lot of recompilation, ccache might help. It doesn't actually speed up the compilation, but it will give you a cached result if you happen to do a useless recompilation for some reason. It might give an impression of tackling the wrong problem, but sometimes the rebuilding rules are so complicated that you actually do end up with the same compilation step during a new build.
Additional idea: if your code compiles with clang, use it instead. It's usually faster than gcc.
This paper describes a method for compiling template code much like "traditional" non-template object files. Saves compile & link time, with only one line of code overhead per template instantiation.