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.
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.
When I do make
, I do not see any speed up. However if I do make -j N
, then it is built definitely faster.
Would you please help me on this issue? (I am developing this on Linux.)
Here is the snippet of the code that I use in 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})
Thank you very much.
Via setting the CMAKE_MAKE_PROGRAM variable you want to affect the build process. But:
This variable affects only the build via
cmake --build
, not on native tool (make
) call:This variable should be a CACHEd one. It is used in such way by make-like generators:
That is, you need to set this variable with
This variable should refer to the executable itself, not to a program with arguments:
That is, value "make -j 2" cannot be used for that variable (splitting arguments as list
wouldn't help either).
In summary, you may redefine the behavior of
cmake --build
calls with setting the CMAKE_MAKE_PROGRAM variable to the script, which callsmake
with parallel options. But you may not affect the behavior of directmake
calls.