共用方式為


Building Multiple Projects in Parallel with MSBuild

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can use MSBuild to build multiple projects faster by running them in parallel. To run builds in parallel, you use the following settings on a multi-core or multiple processor computer:

  • The /maxcpucount switch at a command prompt.

  • The BuildInParallel task parameter on an MSBuild task.

Note

The /verbosity (/v) switch in a command line can also affect build performance. Your build performance might decrease if the verbosity of your build log information is set to detailed or diagnostic, which are used for troubleshooting. For more information, see Obtaining Build Logs and Command-Line Reference.

/maxcpucount Switch

If you use the /maxcpucount switch, or /m for short, MSBuild can create the specified number of MSBuild.exe processes that may be run in parallel. These processes are also known as "worker processes." Each worker process uses a separate core or processor, if any are available, to build a project at the same time as other available processors may be building other projects. For example, setting this switch to a value of "4" causes MSBuild to create four worker processes to build the project.

If you include the /maxcpucount switch without specifying a value, MSBuild will use up to the number of processors on the computer.

For more information about this switch, which was introduced in MSBuild 3.5, see Command-Line Reference.

The following example instructs MSBuild to use three worker processes. If you use this configuration, MSBuild can build three projects at the same time.

msbuild.exe myproj.proj /maxcpucount:3  

BuildInParallel Task Parameter

BuildInParallel is an optional boolean parameter on a MSBuild task. When BuildInParallel is set to true (its default value), multiple worker processes are generated to build as many projects at the same time as possible. For this to work correctly, the /maxcpucount switch must be set to a value greater than 1, and the system must be at least dual-core or have two or more processors.

The following is an example, taken from microsoft.common.targets, about how to set the BuildInParallel parameter.

<PropertyGroup>  
    <BuildInParallel Condition="'$(BuildInParallel)' ==   
        ''">true</BuildInParallel>  
</PropertyGroup>  
<MSBuild  
    Projects="@(_MSBuildProjectReferenceExistent)"  
    Targets="GetTargetPath"  
    BuildInParallel="$(BuildInParallel)"  
    Properties="%(_MSBuildProjectReferenceExistent.SetConfiguration);   
        %(_MSBuildProjectReferenceExistent.SetPlatform)"  
    Condition="'@(NonVCProjectReference)'!='' and   
        ('$(BuildingSolutionFile)' == 'true' or   
        '$(BuildingInsideVisualStudio)' == 'true' or   
        '$(BuildProjectReferences)' != 'true') and     
        '@(_MSBuildProjectReferenceExistent)' != ''"  
    ContinueOnError="!$(BuildingProject)">  
    <Output TaskParameter="TargetOutputs"   
        ItemName="_ResolvedProjectReferencePaths"/>  
</MSBuild>  

See Also

Using Multiple Processors to Build Projects
Writing Multi-Processor-Aware Loggers
Tuning C++ Build Parallelism blog