从CMake设置'make'默认使用'-j'选项 [英] From CMake setup 'make' to use '-j' option by default
问题描述
每当我从终端调用 make
时,我都希望我的CMake项目由 make -j N
构建。我不想每次都手动设置 -j
选项。
I want my CMake project to be built by make -j N
, whenever I call make
from the terminal. I don't want to set -j
option manually every time.
为此,我设置了特定命令行的CMAKE_MAKE_PROGRAM 变量。我使用 ProcessorCount()
函数,该函数给出并行执行构建的处理器数量。
For that, I set CMAKE_MAKE_PROGRAM variable to the specific command line. I use the ProcessorCount()
function, which gives the number of procesors to perform build in parallel.
当我做 make
,我看不到有任何提高。但是,如果我使用 make -j N
,那么它的构建速度肯定会更快。
When I do make
, I do not see any speed up. However if I do make -j N
, then it is built definitely faster.
请您帮我这个问题? (我正在Linux上进行开发。)
Would you please help me on this issue? (I am developing this on Linux.)
这是我在 CMakeList.txt
:
include(ProcessorCount)
ProcessorCount(N)
message("number of processors: " ${N})
if(NOT N EQUAL 0)
set(CTEST_BUILD_FLAGS -j${N})
set(ctest_test_args ${ctest_test_args} PARALLEL_LEVEL ${N})
set(CMAKE_MAKE_PROGRAM "${CMAKE_MAKE_PROGRAM} -j ${N}")
endif()
message("cmake make program" ${CMAKE_MAKE_PROGRAM})
非常感谢。
推荐答案
通过设置要影响构建过程的 CMAKE_MAKE_PROGRAM 变量。但是:
Via setting the CMAKE_MAKE_PROGRAM variable you want to affect the build process. But:
-
该变量仅通过
cmake --build
,而不是在本地工具(make
)调用中:
This variable affects only the build via
cmake --build
, not on native tool (make
) call:
CMAKE_MAKE_PROGRAM
变量设置为由项目代码使用。该值也由cmake(1)-build
和ctest(1)-build-and-test $ c $使用c>启动本机构建过程的工具。
The
CMAKE_MAKE_PROGRAM
variable is set for use by project code. The value is also used by the cmake(1)--build
and ctest(1)--build-and-test
tools to launch the native build process.
此变量应为 CACHE d一。类似 make 的生成器以这种方式使用它:
This variable should be a CACHEd one. It is used in such way by make-like generators:
这些生成器将CMAKE_MAKE_PROGRAM存储在CMake缓存中,因此
These generators store CMAKE_MAKE_PROGRAM in the CMake cache so that it may be edited by the user.
也就是说,您需要使用
set(CMAKE_MAKE_PROGRAM <program> CACHE PATH "Path to build tool" FORCE)
此变量应引用可执行文件本身,而不是带有参数的程序:
This variable should refer to the executable itself, not to a program with arguments:
该值可以是可执行文件的完整路径,也可以是工具名称(如果期望它位于PATH中)。
The value may be the full path to an executable or just the tool name if it is expected to be in the PATH.
也就是说,值 make -j 2不能用于该变量(将参数拆分为列表
That is, value "make -j 2" cannot be used for that variable (splitting arguments as list
set(CMAKE_MAKE_PROGRAM make -j 2 CACHE PATH "Path to build tool" FORCE)
总而言之,您可能会通过将 CMAKE_MAKE_PROGRAM 变量设置为 script (调用<$ c),重新定义 cmake --build
调用的行为$ c> make 和并行选项。但是您可能不会影响直接拨打
的行为。
In summary, you may redefine the behavior of cmake --build
calls with setting the CMAKE_MAKE_PROGRAM variable to the script, which calls make
with parallel options. But you may not affect the behavior of direct make
calls.
这篇关于从CMake设置'make'默认使用'-j'选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!