# Correct usuage of target\_compile\_option MP for Visual Studio

**URL:** https://discourse.cmake.org/t/correct-usuage-of-target-compile-option-mp-for-visual-studio/2825
**Category:** Usage
**Created:** [February 23, 2021, 8:48pm UTC](https://discourse.cmake.org/t/correct-usuage-of-target-compile-option-mp-for-visual-studio/2825 "2021-02-23T20:48:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Steven\_White](https://discourse.cmake.org/user_avatar/discourse.cmake.org/steven_white/32/1231_2.png) [@Steven\_White](https://discourse.cmake.org/u/Steven_White)
#### Post date: [February 23, 2021, 8:48pm UTC](https://discourse.cmake.org/t/correct-usuage-of-target-compile-option-mp-for-visual-studio/2825/1 "2021-02-23T20:48:41Z")

</div>

According to the documents for MP in the MSVC website MP takes an optional digit to limit the number of parallel compilation units a single target will generate at a time. I wanted to always turn on MP for my users, but allow them to optionally limit the number of threads by using a variable in the cache. So in my bigger libraries we automatically include the following.

```auto
target_compile_options(libbiogears_cdm PRIVATE $<$<PLATFORM_ID:Windows>:/bigobj> PRIVATE $<$<PLATFORM_ID:Windows>:/MP${Biogears_BUILD_PARALLEL_LEVEL}>)

```

The problem I have is that while hard coding a number like /MP5 seems to work the following code seems to just ignore the limiter when it is present and just use all available threading. The second thing I don’t understand is where to look up in the MSVC UI if the correct flag is being sent. When I run the following code. The Properties page `C++ -> Multi-processor Compilation` has a value of yes, but there is no place for the GUI to report the value of the limiter and when I look at the compiler flags `C++->Command Line` MP is the first option but has no parameter

```auto
/MP /GS /W3 /Zc:wchar_t /I"D:\remotes\sed-stash\core\worktrees\libpy\projects\biogears\libBiogears\include" /I"D:\remotes\sed-stash\core\worktrees\libpy\projects\biogears\libBiogears\src" /I"D:\remotes\sed-stash\core\worktrees\libpy\build-msvc16\projects\biogears" /I"D:\remotes\sed-stash\core\worktrees\libpy\build-msvc16\projects\biogears-common" /I"D:\remotes\sed-stash\core\worktrees\libpy\projects\biogears-common\include" /I"D:\remotes\sed-stash\core\worktrees\libpy\projects\biogears\libCDM\include" /I"D:\remotes\sed-stash\external\windows-vc16-amd64\include\eigen3" /I"D:\remotes\sed-stash\external\windows-vc16-amd64\include" /Gm- /O2 /Ob2 /Fd"D:\remotes\sed-stash\core\worktrees\libpy\build-msvc16\outputs\Release\bin\libbiogears.pdb" /Zc:inline /fp:precise /D "WIN32" /D "_WINDOWS" /D "NDEBUG" /D "_SCL_SECURE_NO_WARNINGS" /D "_CRT_SECURE_NO_WARNINGS" /D "NOMINMAX" /D "BIOGEARS_THROW_READONLY_EXCEPTIONS" /D "BIOGEARS_THROW_NAN_EXCEPTIONS" /D "LOG4CPP_HAS_DLL" /D "BIOGEARS_COMMON_BUILD_STATIC" /D "CMAKE_INTDIR=\"Release\"" /D "biogears_EXPORTS" /D "_WINDLL" /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /GR /Gd /MD /std:c++14 /Fa"libbiogears.dir\Release\" /EHsc /nologo /Fo"libbiogears.dir\Release\" /Fp"libbiogears.dir\Release\libbiogears.pch" /diagnostics:column 

```

To further complicate this if I replace ${Biogears\_BUILD\_PARALLEL\_LEVEL} with 5 then after a regeneration step the UI looks the same but the task manager clearly shows only 5 files being compiled at a time. So, I have two questions.

1. Should the variable expansion in a generator expression work as in the above code?
2. Does anyone know what file or where in the UI can confirm the MP limit if one is given to speed up debugging of the issue?

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [February 23, 2021, 10:18pm UTC](https://discourse.cmake.org/t/correct-usuage-of-target-compile-option-mp-for-visual-studio/2825/2 "2021-02-23T22:18:15Z")

</div>

From the docs:

The **/MP** option can reduce the total time to compile the source files on the command line. The **/MP** option causes the compiler to create one or more copies of itself, each in a separate process. Then these copies simultaneously compile the source files. Consequently, the total time to build the source files can be significantly reduced.

## Syntax

> **/MP** [_processMax_]

## Arguments

_processMax_  
(Optional) The maximum number of processes that the compiler can create.

The _processMax_ argument must range from 1 through 65536. Otherwise, the compiler issues warning message **D9014** , ignores the _processMax_ argument, and assumes the maximum number of processes is 1.

If you omit the _processMax_ argument, the compiler retrieves the number of [effective processors](https://docs.microsoft.com/en-us/cpp/build/reference/mp-build-with-multiple-processes?view=msvc-160#effective_processors) on your computer from the operating system, and creates a process for each processor.

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [February 24, 2021, 11:59am UTC](https://discourse.cmake.org/t/correct-usuage-of-target-compile-option-mp-for-visual-studio/2825/5 "2021-02-24T11:59:38Z")

</div>

> [@Steven\_White](#):
>
> 1. Should the variable expansion in a generator expression work as in the above code?
> 2. Does anyone know what file or where in the UI can confirm the MP limit if one is given to speed up debugging of the issue?

1. Yes, it should work. But note that the expansion will happen _immediately_, i.e. at configure time when the generator expression is defined, not at generate time when it’s epxanded & used. So the variable must be set correctly at the time the `target_compile_options()` call happens. I would double-check that the variable holds what you think it does at that point.

2. I can confirm I can’t see the number anywhere in the UI either. However, it’s present in the generated `.vcxproj` in a `<ProcessorNumber>` element, so you can check that using a text editor or XML viewer.

---

<div class="post-metadata">

### Author: ![Steven\_White](https://discourse.cmake.org/user_avatar/discourse.cmake.org/steven_white/32/1231_2.png) [@Steven\_White](https://discourse.cmake.org/u/Steven_White)
#### Post date: [February 27, 2021, 3:48pm UTC](https://discourse.cmake.org/t/correct-usuage-of-target-compile-option-mp-for-visual-studio/2825/6 "2021-02-27T15:48:49Z")

</div>

Thanks, I’ll review the proj file for that field and see what is going on. I did not know a out the other control variable now I have to figure out which I actually want. I have found MP almost linearly scales so I think that’s right, but I’ll test the other as well

Awsome responses.
