How to speed up g++ compile time (when using a lot of templates)

后端 未结 10 1147
悲&欢浪女
悲&欢浪女 2020-12-22 18:46

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 h

相关标签:
10条回答
  • 2020-12-22 19:21

    If there are a lot of files you can speed up compilation a lot by just having one .cpp file that #includes all the other .cpp files. This of course requires you to be more careful with macros and such that you already have defined per file as they will now be visible to other cpp files.

    If there are many files this can reduce compile time a lot.

    0 讨论(0)
  • 2020-12-22 19:32

    Here's what I've done to speed up builds under a very similar scenario that you describe (boost, templates, gcc)

    • build on local disk instead of a network file system like NFS
    • upgrade to a newer version of gcc
    • investigate distcc
    • faster build systems, especially more RAM
    0 讨论(0)
  • 2020-12-22 19:33

    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.

    0 讨论(0)
  • 2020-12-22 19:34

    What has been most useful for me:

    • Build on a RAM filesystem. This is trivial on Linux. You may want to keep a copy of common header files (precompiled or the actual .h files) on the RAM filesystem as well.
    • Precompiled headers. I have one per (major) library (e.g. Boost, Qt, stdlib).
    • Declare instead of include classes where possible. This reduces dependencies, thus reduces the number of files which need to be recompiled when you change a header file.
    • Parallelize make. This usually helps on a case-by-case basis, but I have -j3 globally for make. Make sure your dependency graphs are correct in your Makefile, though, or you may have problems.
    • Use -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).
    • Compile each time you save. Some people don't like this, but it allows you to see errors early and can be done in the background, reducing the time you have to wait when you're done writing and ready to test.
    0 讨论(0)
提交回复
热议问题