如何在 CMake 中配置便携式并行构建? [英] How do I configure portable parallel builds in CMake?

查看:27
本文介绍了如何在 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) Build a Project (cmake --build) 获得了 --parallel []-j [] 选项来指定并行构建级别.它们映射到本机构建工具的相应选项.

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:

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

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