Speed up compilation/link time when using boost libraries

前端 未结 2 1831
臣服心动
臣服心动 2021-02-19 17:53

I\'m using Boost Program Options, and it takes quite a while (10 seconds or even more) for compiling very small C++ code with it. It took 1 second compiling the code without boo

2条回答
  •  梦如初夏
    2021-02-19 18:33

    There isn't really much you can do beyond the usual tricks:

    • minimize dependencies: pull in only the Boost headers you really need, and use as specific headers as possible (many libraries have a single "master" header, such as boost/thread.hpp, but also a subdirectory with specific headers, like boost/thread/shared_mutex.hpp),
    • where possible, rely on forward declarations instead of including the entire header,
    • if possible, include the header only in a .cpp file. If you include it in a header, it has to be compiled every time a translation unit which includes that header is compiled. As a general rule of thumb, try to minimize the amount of code you have in headers,
    • all major compilers support precompiled headers. Use those to cut down compilation time,
    • experiment with unity builds. That may or may not be an advantage in your case.

    And last, but not least, a final option is just to not use those specific Boost libraries.

    I sometimes use certain Boost libs early on, out of convenience, and if/when compilation time gets too bad, I start looking at which libraries are expensive to compile, and which ones could be replaced by relatively simple code. Often, Boost is encumbered by the requirement to be so general. If you don't need something that works on 8 year old compilers, or which doesn't have to work across quite so many different types, then you may be able to write a simple replacement which works for you, and takes almost no time to compile.

提交回复
热议问题