如何在CMake中配置可移植并行构建? [英] How do I configure portable parallel builds in CMake?

查看:351
本文介绍了如何在CMake中配置可移植并行构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论使用哪种构建工具,是否都能以某种方式进行并行构建?

Is it somehow possible to be able to have a parallel build no matter which build tool is used?

在Unix下,我们可以添加 make -jN 其中N是线程数,在Windows下,我添加到 CXX_FLAG / MP ,然后在Visual Studio中使用它来并行构建...(?)如何运行CMake时不总是扩展 CMAKE_MAKE_PROGRAM 的版本?

Under Unix we can add make -jN where N are the number of threads, and under Windows I added to the CXX_FLAG "/MP" which is then used in Visual Studio to parallel build...(?) How can I make my version such that CMAKE_MAKE_PROGRAM is not always extended when I run CMake?

什么是通用解决方案?

我想出了这一点:

# Add some multithreaded build support
MARK_AS_ADVANCED(MULTITHREADED_BUILD)
set(MULTITHREADED_BUILD 12 CACHE STRING "How many threads are used to build the project")
if(MULTITHREADED_BUILD)
    if(${CMAKE_GENERATOR} MATCHES "Unix Makefiles")
            message(STATUS ${CMAKE_BUILD_TOOL})
            set(CMAKE_MAKE_PROGRAM "${CMAKE_MAKE_PROGRAM} -j${MULTITHREADED_BUILD}")
            message(STATUS "Added arguments to CMAKE_BUILD_TOOL: ${CMAKE_MAKE_PROGRAM}")
    elseif(MSVC)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
      message(STATUS "Added parallel build arguments to CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
    endif()
endif()


推荐答案

对于CMake 3.12,这是可能的。从发行说明

With CMake 3.12 this is possible. From the release notes:


cmake(1)建立项目( cmake --build )获得了-平行的[< jobs>] -j [< jobs>] 选项以指定并行构建级别。它们映射到本机构建工具的相应选项。

The cmake(1) Build a Project (cmake --build) gained --parallel [<jobs>] and -j [<jobs>] options to specify a parallel build level. They map to corresponding options of the native build tool.

如dkg 所述,您还可以设置环境变量 CMAKE_BUILD_PARALLEL_LEVEL

As mentioned by dkg, you can also set the environment variable CMAKE_BUILD_PARALLEL_LEVEL.

链接到CMake的文档:

Links to CMake's documentation:

  • Build a Project
  • CMAKE_BUILD_PARALLEL_LEVEL

这篇关于如何在CMake中配置可移植并行构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆