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

后端 未结 10 1207
悲&欢浪女
悲&欢浪女 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:11

    I assume that we are talking about minutes to compile a file, i.e. precompiled headers or local disk issues aren't the problem.

    Long compilation times with deep template code (boost etc.) is often rooted in the unfriendly asymptotic behavior of gcc when it comes to template instantiation, in particular when variadic templates are emulated with template default arguments.

    Here's a document which names reduced compilation time as a motivation for variadic templates:

    • http://osl.iu.edu/~dgregor/cpp/variadic-templates.html

    cpptruths had an article about how gcc-4.5 is much better in this behalf and how it does brilliantly with its variadic templates:

    • http://cpptruths.blogspot.com/2010/03/faster-meta-programs-using-gcc-45-and.html

    IIRC then BOOST has a way to limit the generation of template default parameters for the pseudo-variadics, I think 'g++ -DBOOST_MPL_LIMIT_LIST_SIZE=10' should work (the default is 20)

    UPDATE: There is also a nice thread with general techniques to speed up compiling here on SO which might be useful:

    • What techniques can be used to speed up C++ compilation times?

    UPDATE: This one is about the performance issues when compiling templates, the accepted answer recommends gcc-4.5 too, also clang is mentioned as a positive example:

    • Are there optimized c++ compilers for template use?

提交回复
热议问题