# How to Enable CMake parallel compile

**URL:** https://discourse.cmake.org/t/how-to-enable-cmake-parallel-compile/13265
**Category:** Code
**Tags:** os:windows, gen:makefiles, comp:mingw
**Created:** [December 26, 2024, 2:28am UTC](https://discourse.cmake.org/t/how-to-enable-cmake-parallel-compile/13265 "2024-12-26T02:28:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ElbertZ](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/e/e9c0ed/32.png) [@ElbertZ](https://discourse.cmake.org/u/ElbertZ)
#### Post date: [December 26, 2024, 2:28am UTC](https://discourse.cmake.org/t/how-to-enable-cmake-parallel-compile/13265/1 "2024-12-26T02:28:27Z")

</div>

I try to parallel compile a c++ project with Visual Studio 2022. However It cost 4 minute in Visual Studio2022 or using command instead of 1minute in VSCode, VisualStudio2022 and using command will cost more time and less CPU,which is likely unable parallel compile.

Below is the environment and cmake configure:  
CMakeList.txt:

```auto
cmake_minimum_required(VERSION 3.22)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_GENERATOR	"MinGW Makefiles")
set(CMAKE_BUILD_PARALLEL_LEVEL 12)
find_program(CMAKE_CXX_COMPILER "${mingw_tools}/bin/g++.exe" )
find_program(CMAKE_C_COMPILER "${mingw_tools}/bin/gcc.exe" )		
find_program(CMAKE_MAKE_PROGRAM "${mingw_tools}/bin/mingw32-make.exe")

```

The command I use to test parallel compiling, but it is still cost 4 minute:

```auto
cmake --preset x64-debug -B build
cmake --build build --config Debug

```

However Using same CmakeLists.txt file with VSCode, I compile the project only cost 1 minute,

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [December 26, 2024, 6:00am UTC](https://discourse.cmake.org/t/how-to-enable-cmake-parallel-compile/13265/2 "2024-12-26T06:00:32Z")

</div>

> [@ElbertZ](#):
>
> ```auto
> set(CMAKE_GENERATOR	"MinGW Makefiles")
> 
> ```

You can’t set the generator this way. The project cannot choose the generator. The user selects it with the `-G` option when running the `cmake` executable, or as part of a CMake preset. An example of setting it via the command line:

```auto
cmake -G "MinGW Makefiles" ...

```

> [@ElbertZ](#):
>
> ```auto
> set(CMAKE_BUILD_PARALLEL_LEVEL 12)
> 
> ```

This also can’t be set by the project. `CMAKE_BUILD_PARALLEL_LEVEL` needs to be set as an environment variable when running the build step using `cmake --build`. You can also set this in a CMake preset, but projects shouldn’t do that because they can’t know the capabilities of the developer’s machine.

> [@ElbertZ](#):
>
> ```auto
> set(CMAKE_CXX_STANDARD 11)
> set(CMAKE_CXX_STANDARD_REQUIRED ON)
> 
> ```

These should generally only be set after the first `project()` call (this is also missing from your example).

> [@ElbertZ](#):
>
> ```auto
> find_program(CMAKE_CXX_COMPILER "${mingw_tools}/bin/g++.exe" )
> find_program(CMAKE_C_COMPILER "${mingw_tools}/bin/gcc.exe" )		
> 
> ```

These should not be set by the project. They should be set in a toolchain file instead. They can be set on the command line or in a CMake preset, but I’d recommend using a toolchain file instead.

> [@ElbertZ](#):
>
> `find_program(CMAKE_MAKE_PROGRAM "${mingw_tools}/bin/mingw32-make.exe")`

This also shouldn’t be set by the project. It should be set by the user on the `cmake` command line.

---

<div class="post-metadata">

### Author: ![ElbertZ](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/e/e9c0ed/32.png) [@ElbertZ](https://discourse.cmake.org/u/ElbertZ)
#### Post date: [December 30, 2024, 6:50am UTC](https://discourse.cmake.org/t/how-to-enable-cmake-parallel-compile/13265/3 "2024-12-30T06:50:49Z")

</div>

Actually，It is part of my CMakeLists.txt file. I used to set CMAKE\_BUILD\_PARALLEL\_LEVEL on preset, but parallel compiling still is not working.
