6

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.

janekb04
  • 4,304
  • 2
  • 20
  • 51

1 Answers1

10

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%
Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • 2
    Thanks! I didn’t know that using the Ninja generator would automatically deal with this. I will just switch to using it as this seems to me to be the easiest, least error-prone and most future-proof solution out of the three. – janekb04 Jan 02 '22 at 20:42
  • 1
    It was really worth it. Now a full rebuild of my project project takes only a few seconds, rather than a few minutes. And linking is nearly instantaneous. – janekb04 Jan 03 '22 at 08:17
  • CMake Warning: Ignoring extra path from command line: – Nicholas Jela Oct 09 '22 at 09:02