Is it somehow possible to be able to have a parallel build no matter which build tool is used?
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?
What is a general solution?
I came up with this:
# 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()
You could force CMake to reconfigure every time e.g. by calling make rebuild_cache before your actual build or by adding e.g. add_custom_command(TARGET MyExe POST_BUILD ${CMAKE_COMMAND} -E remove ${CMAKE_CURRENT_BINARY_DIR}/version.
Running CMake from the command line From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.
Run the cmake executable or the cmake-gui to configure the project and then build it with your chosen build tool. Run the install step by using the install option of the cmake command (introduced in 3.15, older versions of CMake must use make install ) from the command line, or build the INSTALL target from an IDE.
With CMake 3.12 this is possible. From the release notes:
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.
As mentioned by dkg, you can also set the environment variable CMAKE_BUILD_PARALLEL_LEVEL
.
Links to CMake's documentation:
If you have CMake v2.8.8 or higher, you may use Ninja as an alternative of GNU make
:
mkdir build cd build cmake -G Ninja .. ninja # Parallel build (no need -j12)
or
mkdir build cd build cmake -G Ninja .. cmake --build . # Parallel build using Ninja
As you can see, no need to use CMAKE_MAKE_PROGRAM
, the build is run in parallel by default, optimizing the number of jobs depending on available CPU cores.
Ninja is based on a low-level JSON configuration to speed up the startup phase. Therefore its JSON configuration is not easy to write by hand, and I always generate it using a high-level tool/IDE:
As a C++ build often requires lots of memory, your computer must provide as much memory as the number of CPU cores.
As pointed out by Ruslan, CMake 3.12 (2018) has a new option cmake --build -j <N>
to limit build to <N>
cores (jobs) thus limiting the memory consumption (see also the documentation). If you use an older CMake version, you can still use cmake --build -- -j <N>
. The option --
tells to CMake to pass the rest directly to the underlying builder tool, here it is Ninja.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With