本文介绍了如何加速我的CMake启用C ++项目的编译时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了几个SO问题关于改善CMake启用C ++项目的最近的周转时间的特定方面(如或),我想知道是否有更一般的指导利用具体的可能性CMake提供。如果可能没有跨平台编译时优化,我主要对Visual Studio或基于GNU toochain的方法感兴趣。



我已经知道并投资进入通常推荐的区域以加速C ++构建:


  1. 更改/优化/微调工具链

    / li>
  2. 优化您的代码库/软件架构(例如通过减少依赖关系并使用明确定义的子项目 - 单元测试)


  3. 投资更好的硬件(SSD,CPU,内存)


=http://stackoverflow.com/questions/373142/what-techniques-can-be-used-to-speed-up-c-compilation-times>此处,或。所以我在这个问题的焦点是第一点。



另外我知道在CMake的维基中找到的建议:








前者仅处理基本(并行make),后面的处理主要是如何加速解析CMake文件。



只是为了使这个更具体,如果我从与100个库使用MSYS / GNU我得到以下时间测量结果:

$ bake

  $ cmake --version 
cmake版本3.5.2
Kitware维护和支持的CMake套件)。

$ time -p cmake -GMSYS Makefiles..
- CXX编译器标识为GNU 4.8.1
...
- 配置done
- 生成完成
- 生成文件已写入:[...]
real 27.03
用户0.01
sys 0.03

$ time -p make -j8
...
[100%]内置目标CMakeTest
real 113.11
用户8.82
sys 33.08

所以我总共有约140秒钟,我的目标 - 这个承认很简单的例子 -

解决方案

这里是我有很好的结果与我的标准设置/工具。使用CMake和Visual Studio或GNU工具链:


  1. 使用。它更快,使用所有可用的CPU核心自动和有良好的依赖关系管理。只需要注意



    a。)您需要在CMake中正确设置目标依赖项。如果你得到一个点,其中构建有对另一个工件的依赖,它必须等待,直到那些被编译(同步点)。

      $ time -p cmake -GNinja.. 
    - CXX编译器标识是GNU 4.8.1
    ...
    real 11.06
    用户0.00
    sys 0.00

    $ time -p ninja
    ...
    [202/202]链接CXX可执行文件CMakeTest.exe
    real 40.31
    用户0.01
    sys 0.01

    b)链接总是这样的同步点。因此,您可以更多地使用CMake的减少这些,但它使你的CMake代码有点丑。

      $ time -p ninja 
    ...
    [102/102]链接CXX可执行文件CMakeTest.exe
    real 27.62
    用户0.00
    sys 0.04


  2. 将较少更改或稳定的代码部分拆分成单独的CMake项目,并使用CMake的或 - 如果您切换到一些库的二进制传送 - 。


  3. 考虑为您的日常工作设置不同的编译器/链接器选项测试时间/使用最终版本构建选项的体验)。



    a)跳过优化部分



    b 。)尝试增量链接


  4. 如果您经常对CMake代码本身进行更改,请考虑从为您的机器架构优化的源重建CMake。



    当我使用MinGW64 / MSYS重新编译CMake 3.5.2时,例如

      cmake -DCMAKE_BUILD_TYPE:STRING =Release
    -DCMAKE_CXX_FLAGS:STRING = - march = native -m64 -Ofast -flto
    -DCMAKE_EXE_LINKER_FLAGS:STRING = - Wl, - allow-multiple-definition
    -GMSYS Makefiles..

    我可以加速第一部分:

      $ time -p [... ] /MSYS64/bin/cmake.exe -GNinja.. 
    real 6.46
    用户0.03
    sys 0.01


  5. 如果您的文件I / O非常慢,并且由于CMake使用专用的二进制输出目录,请使用RAM磁盘。

  6. 根据最终输出文件,将GNU标准链接器与


参考 b





I came across several SO questions regarding specific aspects of improving the turn-around time of CMake enabled C++ projects lately (like "At what level should I distribute my build process?" or "cmake rebuild_cache for just a subdirectory?"), I was wondering if there is a more general guidance utilizing the specific possibilities CMake offers. If there is probably no cross-platform compile time optimization, I'm mainly interested in Visual Studio or GNU toochain based approaches.

And I'm already aware of and investing into the generally recommended areas to speed up C++ builds:

  1. Change/Optimize/fine-tune the toolchain

  2. Optimize your code base/software architecture (e.g by reducing the dependencies and use well-defined sub-projects - unit tests)

  3. Invest in a better hardware (SSD, CPU, memory)

like recommended here, here or here. So my focus in this question is on the first point.

Plus I know of the recommendations to be found in CMake's Wiki:

The former just handles the basics (parallel make), the later handles mostly how to speed-up parsing CMake files.

Just to make this a little more concrete, if I take my CMake example from here with 100 libraries using MSYS/GNU I got the following time measurement results:

$ cmake --version
cmake version 3.5.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).

$ time -p cmake -G "MSYS Makefiles" ..
-- The CXX compiler identification is GNU 4.8.1
...
-- Configuring done
-- Generating done
-- Build files have been written to: [...]
real 27.03
user 0.01
sys 0.03

$ time -p make -j8
...
[100%] Built target CMakeTest
real 113.11
user 8.82
sys 33.08

So I have a total of ~140 seconds and my goal - for this admittedly very simple example - would be to get this down to about 10-20% of what I get with the standard settings/tools.

解决方案

Here's what I had good results with using CMake and Visual Studio or GNU toolchains:

  1. Exchange GNU make with Ninja. It's faster, makes use of all available CPU cores automatically and has a good dependency management. Just be aware of

    a.) You need to setup the target dependencies in CMake correctly. If you get to a point where the build has a dependency to another artifact, it has to wait until those are compiled (synchronization points).

    $ time -p cmake -G "Ninja" ..
    -- The CXX compiler identification is GNU 4.8.1
    ...
    real 11.06
    user 0.00
    sys 0.00
    
    $ time -p ninja
    ...
    [202/202] Linking CXX executable CMakeTest.exe
    real 40.31
    user 0.01
    sys 0.01
    

    b.) Linking is always such a synchronization point. So you can make more use of CMake's Object Libraries to reduce those, but it makes your CMake code a little bit uglier.

    $ time -p ninja
    ...
    [102/102] Linking CXX executable CMakeTest.exe
    real 27.62
    user 0.00
    sys 0.04
    

  2. Split less frequently changed or stable code parts into separate CMake projects and use CMake's ExternalProject_Add() or - if you e.g. switch to binary delivery of some libraries - find_library().

  3. Think of a different set of compiler/linker options for your daily work (but only if you also have some test time/experience with the final release build options).

    a.) Skip the optimization parts

    b.) Try incremental linking

  4. If you often do changes to the CMake code itself, think about rebuilding CMake from sources optimized for your machine's architecture. CMake's officially distributed binaries are just a compromise to work on every possible CPU architecture.

    When I use MinGW64/MSYS to rebuild CMake 3.5.2 with e.g.

    cmake -DCMAKE_BUILD_TYPE:STRING="Release"
          -DCMAKE_CXX_FLAGS:STRING="-march=native -m64 -Ofast -flto"
          -DCMAKE_EXE_LINKER_FLAGS:STRING="-Wl,--allow-multiple-definition"
          -G "MSYS Makefiles" ..
    

    I can accelerate the first part:

    $ time -p [...]/MSYS64/bin/cmake.exe -G "Ninja" ..
    real 6.46
    user 0.03
    sys 0.01
    

  5. If your file I/O is very slow and since CMake works with dedicated binary output directories, make use of a RAM disk.

  6. Depending of your final output file, exchange the GNU standard linker with the Gold Linker

References

这篇关于如何加速我的CMake启用C ++项目的编译时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 16:03