94
cmake --build . --config Release

Is it possible to set the number of cores to be used by the build process?

I'm looking for something similar to GNU make's -j option.

Boann
  • 48,794
  • 16
  • 117
  • 146
Konstantin Burlachenko
  • 5,233
  • 2
  • 41
  • 40

2 Answers2

106

According to the Release Notes, with CMake 3.12 it can be done cross-platform:

The cmake(1) Build Tool Mode (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.

starball
  • 20,030
  • 7
  • 43
  • 238
usr1234567
  • 21,601
  • 16
  • 108
  • 128
89

You can pass arbitrary arguments to the native build tool with --. Everything after -- will be passed to the build tool. To pass -j 3 in your example, just use

cmake --build . --config Release -- -j 3

Documentation: https://cmake.org/cmake/help/v3.5/manual/cmake.1.html

You could also use Ninja as a build tool, it uses automatically an appropriate number of threads. Or you can modify the make command by defining CMAKE_MAKE_PROGRAM="make -j 3. But this is a rather hacky workaround.

usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • 6
    Is there an build-tool independent way to do this? – Knitschi May 05 '16 at 10:52
  • 2
    No, everything after `--` is passed to the build tool. Maybe your build tools accept the same options, but in general I doubt that. – usr1234567 May 05 '16 at 10:53
  • 2
    Both Windows (cmake 3.7) and Ubuntu (cmake 3.9) report that -j is not an option. Am I doing something wrong? – Matt Jan 22 '18 at 14:31
  • I don't think so.. My comment is basically that this answer doesn't seem to work, or there's something missing, or ultimately: one can't expect to build with multiple cores using only what you've written here in your answer. What generator is assumed to have been used in your answer? – Matt Jan 22 '18 at 15:08
  • 2
    My experience using `-j` on Ubuntu Bash on Windows: The higher the thread limit, the more random compilation errors about missing files I get. This does not seem to work perfectly with `make` on Windows. – mattmilten Jun 13 '18 at 20:56
  • 1
    @Knitschi With CMake 3.12 and newer there is a way, see my other answer https://stackoverflow.com/a/50883555/2799037 – usr1234567 Feb 27 '20 at 09:50
  • The command posted does not work. You should do cmake --build . --config Release --parallel – thecatbehindthemask Jul 16 '21 at 16:24
  • @xhustango Your suggestion is my other answer and only works since CMake 3.12. With make this answer works, if it does not work, you have to pass the right arguments for your build tool at hand. – usr1234567 Jul 18 '21 at 19:34
  • @usr1234567 oh okay! I did not see it! – thecatbehindthemask Jul 19 '21 at 12:54
  • You don't even need the `--`, just use `-j`; this works on at least v3.17 and probably a while before that too. This is actually in the man page (`--parallel -j`). – CodeClown42 Feb 21 '22 at 21:27
  • @CodeClown42 Sure, since CMake 3.12, see my other answer. – usr1234567 Feb 22 '22 at 05:49