optimizationcmakebuildconcurrency

How to build a CMake project in parallel on all available cores?


This related question shows how to build a CMake project using a specified numbers of cores. For example if I wanted to use 10 cores, I could invoke CMake like this:

cmake --build . -j 10

My question is: how can I build using all my available cores. I effectively want CMake to autodetect my core count and use all of them.


Solution

  • As of CMake 3.22, there is no standard way to do this. However, there are a few practical approaches.

    1. If you use the Ninja or Ninja Multi-Config generators, on any platform, simply running the build with cmake --build /path/to/build-dir will use all cores.
    2. If you are on a UNIX-like command line, you can run cmake --build . -j $(nproc) or the same: cmake --build . --parallel $(nproc):
    3. If you are on Windows/cmd, you can run cmake --build . -j %NUMBER_OF_PROCESSORS%