How should I detect bottleneck of compile time in a large C++ project?

后端 未结 4 1424
暖寄归人
暖寄归人 2021-02-18 15:41

I want to reduce compile time of a large C++ project. I tried to use precompiled headers, interface and etc. But before I move on, I want to know whether any tool which helps de

4条回答
  •  余生分开走
    2021-02-18 16:10

    C++ not being modular (yet), compilation bottlenecks are often due to include issues; that is using including too many files when they are not needed. It is also possible that those includes are needed at the moment, but could become superfluous with some simple reengineering.

    • to detect superfluous includes, you can check include-what-you-use, the only issue you'll have is that it works on top of Clang, so you'll need some setup there.
    • otherwise, you need to review your code, and specifically the headers.

    Since the tool is self-sufficient and documented, let me expand a bit on the review process.

    1. Any header that has more than a couple #include is highly suspicious.
    2. On the contrary, if you have a source file chock-full of various types and functions and it only has a couple includes, it probably means that one of the headers brings too much.

    If you have trouble knowing what is required, what is not, and how to remove superfluous headers, I recommend a reading of Pimpls - Beauty Marks You Can Depend On; if you do not know what a Pimpl is, read Compilation Firewalls. I would advise cautiousness though, Pimpl has a runtime and maintenance cost, so only use it when it really is necessary. Personally I would absolutely recommend it in the public headers of a library you deliver to 3rd party (ABI compatibility), and otherwise try to avoid it.

    If manual inspection is not your forte, you can generate the preprocessor output for each header (do not worry about source files too much), and check the bigger outputs.

提交回复
热议问题