# Cmake parallel build

**URL:** <https://discourse.cmake.org/t/cmake-parallel-build/1838>\
**Category:** Code\
**Created:** [September 14, 2020, 12:46pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838 "2020-09-14T12:46:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 14, 2020, 12:46pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/1 "2020-09-14T12:46:12Z")

</div>

How do I set an option for a parallel build?  
My approach so far:  
add\_library(myLib ${SRCS} ${HDRS})  
add\_custom\_command(TARGET myLib COMMAND ${CMAKE\_COMMAND} -j5})

It doesnt have any effect like this. Any other suggestion ?

---

<div class="post-metadata">

**Author:** ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)\
**Post date:** [September 14, 2020, 2:23pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/2 "2020-09-14T14:23:34Z")

</div>

Parallel build is a build tool parameter. Depending on which generator you use you have nothing to do (ninja) or you may specify it.  
You may read this article to know more: [https://blog.kitware.com/cmake-building-with-all-your-cores/](https://blog.kitware.com/cmake-building-with-all-your-cores/)

---

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 14, 2020, 2:25pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/3 "2020-09-14T14:25:02Z")

</div>

Thanks. Ive read it already. It says nothing of the way, I would like to trigger the parallel build.

I would like to force a parallel build for `make` (not ninja or sth else) over cmake

---

<div class="post-metadata">

**Author:** ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)\
**Post date:** [September 14, 2020, 2:56pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/4 "2020-09-14T14:56:24Z")

</div>

It may be that using `cmake --build . -j` is sufficient for the level of parallelism you are looking for.

---

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 14, 2020, 2:58pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/5 "2020-09-14T14:58:00Z")

</div>

It would have been, if the above custom\_command is the way to achieve it ?

---

<div class="post-metadata">

**Author:** ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)\
**Post date:** [September 14, 2020, 3:07pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/6 "2020-09-14T15:07:14Z")

</div>

I just type `cmake --build . -j` at the command line when I want to build. You mentioned using `make`; you would just type `make -j`. There is no need for add `-j` via a custom command.

---

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 14, 2020, 3:09pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/7 "2020-09-14T15:09:18Z")

</div>

Right, I mentioned I do **not** want to type **over the command line,** but over a `CMAKE_CUSTOM_COMMAND` or whatever else possible. So, any suggestion of best practices of doing it, would be appreciated.

---

<div class="post-metadata">

**Author:** ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)\
**Post date:** [September 14, 2020, 3:34pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/8 "2020-09-14T15:34:42Z")

</div>

> [@ProTur1](#):
>
> I would like to force a parallel build for `make` (not ninja or sth else) over cmake

I don’t think you can do that but you can call `make -j 5` or `cmake --build -j5` the latter may use an environment var:  
[https://cmake.org/cmake/help/latest/envvar/CMAKE\_BUILD\_PARALLEL\_LEVEL.html](https://cmake.org/cmake/help/latest/envvar/CMAKE_BUILD_PARALLEL_LEVEL.html)

---

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 14, 2020, 3:36pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/9 "2020-09-14T15:36:17Z")

</div>

Thanks, but as I mentioned already, Im looking for a solution, **not using the command line**.

---

<div class="post-metadata">

**Author:** ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)\
**Post date:** [September 14, 2020, 3:37pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/10 "2020-09-14T15:37:27Z")

</div>

> [@ProTur1](#):
>
> Thanks, but as I mentioned already, Im looking for a solution, **not using the command line**.

How do you trigger your build?  
you said:

> [@ProTur1](#):
>
> I would like to force a parallel build for `make`

so you use `make` but don’t call it?  
I miss something.

---

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 14, 2020, 4:45pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/11 "2020-09-14T16:45:41Z")

</div>

I would like to set Generator Unix Makefiles, and than tell make to use the -j N parallel option.

---

<div class="post-metadata">

**Author:** ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)\
**Post date:** [September 14, 2020, 5:17pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/12 "2020-09-14T17:17:06Z")

</div>

You do not really answer my question.

- If you do invoke `make` then you may define make options in your environment `MAKEFLAGS=-j5`
- if you do invoke `cmake --build` then you may `CMAKE_BUILD_PARALLEL_LEVEL=5`

In any case this is a user choice so such config ought to stay in user’s hand not in the CMakeLists.txt

You may invoke such parallel build using a `custom_target` if you prefer that:

```cmake
add_custom_target(my_parallel_build 
                      COMMAND ${CMAKE_COMMAND} --build -j 5
                      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
                      COMMENT "My parallel build with 5 cores")

```

and then you would invoke:  
`make my_parallel_build`

---

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 14, 2020, 5:23pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/13 "2020-09-14T17:23:20Z")

</div>

I didnt know how to answer better your questions.

My goal is, that if a user sets to use a `Unix Makefiles generator`, and he if he forgets to set the `parallel build flag`, that than I add this flag over `CMake`.

The question is, if this is possible, and if yes, what is the way?

---

<div class="post-metadata">

**Author:** ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)\
**Post date:** [September 14, 2020, 5:58pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/14 "2020-09-14T17:58:56Z")

</div>

> [@ProTur1](#):
>
> My goal is, that if a user sets to use a `Unix Makefiles generator` , and he if he forgets to set the `parallel build flag` , that than I add this flag over `CMake` .
> 
> The question is, if this is possible, and if yes, what is the way?

It is not currently possible to enforce parallelism of the build from within CMakeLists.txt.

---

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 14, 2020, 6:07pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/15 "2020-09-14T18:07:10Z")

</div>

Except for setting the flag for Win VS. For Unix Makefile it is not supported.

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [September 17, 2020, 4:30pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/16 "2020-09-17T16:30:22Z")

</div>

> [@ProTur1](#):
>
> Except for setting the flag for Win VS.

If you are referring to `/MP`, that’s a flag of the compiler, not of the build tool.

GCC and clang simply don’t have such a flag. You could make a feature request on these projects.

---

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 17, 2020, 4:35pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/17 "2020-09-17T16:35:30Z")

</div>

> [@McMartin](#):
>
> `/MP` , that’s a flag of the compiler

Wasnt aware of it. Part of the compiler? So the MS compiler is also a build tool and it parallelizes its compilation process? Interesting.

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [September 17, 2020, 6:44pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/18 "2020-09-17T18:44:03Z")

</div>

The build tool used by Visual Studio is MSBuild. But you can also use MSVC (the compiler) with Ninja, or NMake Makefiles.

When you pass `/MP` and several input files to MSVC, it “multiplies” itself to run in parallel. MSVC doesn’t track modification dates and dependencies, it is not a build tool.

---

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 24, 2020, 10:56am UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/19 "2020-09-24T10:56:04Z")

</div>

I have to ask again, why isnt it possible to set some `BUILD_ARGS` over the `CMakeLists.txt`?

It doesnt seem to me technically difficult, to set there a variable, which is expanded after `cmake --build` to .e.g. an additional `--parallel N` or `--j N` as I would like to in my case.

---

<div class="post-metadata">

**Author:** ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)\
**Post date:** [September 24, 2020, 2:44pm UTC](https://discourse.cmake.org/t/cmake-parallel-build/1838/20 "2020-09-24T14:44:50Z")

</div>

From the CMake documentation for `--parallel [<jobs>], -j [<jobs>]`:  
The [`CMAKE_BUILD_PARALLEL_LEVEL`](https://cmake.org/cmake/help/v3.18/envvar/CMAKE_BUILD_PARALLEL_LEVEL.html#envvar:CMAKE_BUILD_PARALLEL_LEVEL) environment variable, if set, specifies a default parallel level when this option is not given.

[Next page](https://discourse.cmake.org/t/cmake-parallel-build/1838.md?page=2)
