janekb04
janekb04

Reputation: 4925

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.

Upvotes: 11

Views: 13390

Answers (1)

Alex Reinking
Alex Reinking

Reputation: 19916

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)
  3. If you are on Windows/cmd, you can run cmake --build . -j %NUMBER_OF_PROCESSORS%

Upvotes: 23

Related Questions