Pass /MP option to the compiler using MSBuild

2019-07-23 14:34发布

问题:

I have a VC project file that I'm building from command line using MSBuild. I want to specify the /MP flag without editing the project file. Is that possible? I've tried set CL=/MP prior to calling MSBuild, but it has no effect.

回答1:

This can be accomplished by accessing the CL_MPCount Visual Studio option:

MSBuild /m:2 /p:CL_MPCount=2 /p:Configuration=Release tf_tutorials_example_trainer.vcxproj

The above instructs the compiler to perform a maximum of 2 parallel compilation tasks. The /m:2 flag allows MSBuild to build two projects in parallel. The net result is that we have a maximum of 4 cl.exe processes running in parallel.

UPDATE: The CL_MPCount=2 flag gets passed on to cl.exe as /MP2. This allows parallel compilation of 2 .cpp files within the same project.



回答2:

You need a property that you can override from the command line. Open the .vcxproj file in a text editor, Notepad will do. Locate the "Globals" property group and add a property named, say, "Turbo"

  <PropertyGroup Label="Globals">
    <Turbo>false</Turbo>
    ...etc...
  </PropertyGroup>

And use the property to specify the compile option. Since it can only work in the Release build:

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <ClCompile>
      <MultiProcessorCompilation>$(Turbo)</MultiProcessorCompilation>
        ...etc...
    </ClCompile>

And run MSBuild:

  msbuild /p:Configuration=Release /p:Turbo=true