--- title: 加快Visual Studio编译速度 date: 2018-01-05 12:08:32 tags: [visual studio, cplusplus] categories: windows --- 为Visual Studio开启多核编译支持,Visual Studio 2010及以上版本,直接可以在界面配置,而Visual Studio 2008则需要手动增加编译命令。 ## Build with Multiple Processes Visual Studio并行编译选项命令为/MP,具体语法为: > `/MP[processMax]` `processMax` 为可选参数,范围为`1-65536`,表示可以创建的并行进程数,如果省略该参数,默认为[有效内核数](https://msdn.microsoft.com/en-us/library/bb385193.aspx#effective_processors),计算方法为:` (8 effective processors) = (2 physical processors) x (2 cores per physical processor) x (2 effective processors per core because of hyperthreading)`。 `/MP`选项会与下表中的选项冲突,所以如果需要开启`/MP`,需要关闭下表中其他选项,Visual Studio默认配置下debug模式会启用`/Gm`选项,需要手动关闭。 | Option or Language Feature | Description | | ---------------------------------------- | ---------------------------------------- | | [#import](https://msdn.microsoft.com/en-us/library/8etzzkb6.aspx) preprocessor directive | Converts the types in a type library into C++ classes, and then writes those classes to a header file. | | [/E](https://msdn.microsoft.com/en-us/library/3xkfswhy.aspx), [/EP](https://msdn.microsoft.com/en-us/library/becb7sys.aspx) | Copies preprocessor output to the standard output (**stdout**). | | [/Gm](https://msdn.microsoft.com/en-us/library/kfz8ad09.aspx) | Enables an incremental rebuild. | | [/showIncludes](https://msdn.microsoft.com/en-us/library/hdkef6tk.aspx) | Writes a list of include files to the standard error (**stderr**). | | [/Yc](https://msdn.microsoft.com/en-us/library/7zc28563.aspx) | Writes a precompiled header file. | ## 为Visual Studio 2010开启多核并行编译 - 属性 -> 配置属性 -> C/C++ -> 常规 -> 多处理器编译,设为`是`。[^1] - 属性 -> 配置属性 -> C/C++ -> 代码生成 -> 启用最小重新生成,设为`否`。[^2] - 工具 -> 选项 -> 项目和解决方案 -> 生成并运行,设置`最大并行项目生成数`为8(*可以自定义合理值*)。 ## 为Visual Studio 2008开启多核并行编译 - 属性 -> 配置属性 -> C/C++ -> 命令行 -> 附加选项,添加`/MP`。[^1] - 属性 -> 配置属性 -> C/C++ -> 代码生成 -> 启用最小重新生成,设为`否`。[^2] - 工具 -> 选项 -> 项目和解决方案 -> 生成并运行,设置`最大并行项目生成数`为8(*可以自定义合理值*)。 ## 实例说明 以我自己的项目为例,不开启`/MP`选项,完整编译一次时间为48秒,开启之后完整编译一次时间为26秒。 ![after](after.png) ![before](before.png) [^1]: 启用`/MP`编译选项 [^2]: 禁用`/Gm`编译选项