CMake Build Parallel Configuration

Introduction

To use CMake to build a project, usually I will run the following commands brainlessly.

1
2
$ cmake -B build
$ cmake --build build --config Release --parallel

However, it will use all the CPU threads to build the project. If the project is very large, sometimes it will consume all the CPU and memory resources on my local machine and make the system unresponsive or crash. Therefore, it is better to control the parallelism of the CMake build process.

In this blog post, I will discuss quickly how to control the parallelism of the CMake build process.

CMake Build Parallel Configuration

There are two ways to control the parallelism of the CMake build process.

One way is to set the number of jobs for the --parallel option.

1
2
3
$ export NUM_CMAKE_JOBS=4
$ cmake -B build
$ cmake --build build --config Release --parallel ${NUM_CMAKE_JOBS}

The other way is to set the CMAKE_BUILD_PARALLEL_LEVEL environment variable.

1
2
3
$ export NUM_CMAKE_JOBS=4
$ cmake -B build
$ CMAKE_BUILD_PARALLEL_LEVEL=${NUM_CMAKE_JOBS} cmake --build build --config Release

References

Author

Lei Mao

Posted on

09-24-2024

Updated on

09-24-2024

Licensed under


Comments