gpt4 book ai didi

visual-studio - MSBuild - 如何并行构建多个文件和项目

转载 作者:行者123 更新时间:2023-12-04 02:51:19 30 4
gpt4 key购买 nike

我有一个包含大量项目 (.sln) 的大型 Visual Studio 2010 解决方案文件 (~50)。每个项目大致由 20 组成.cpp.h文件。在快速的 Intel i7 机器上构建整个项目大约需要 2 个小时,所有依赖项都本地缓存在快速 SSD 上。

我正在尝试 replicate an existing experiment I found on this topic ,并且需要澄清如何:

  • 获得我正在寻求在 Visual Studio 中工作的并行化(使用 GUI/IDE)。
  • 通过 MSBuild.exe 获取这些更改也适用于自动命令行构建.

  • 首先是 /m/maxcpucount选项一样吗?我有 read an article on the /m option,这似乎建立了更多 项目 在平行下。这似乎与我通过 GUI 中的以下操作设置的选项相同:
  • 工具
  • 选项
  • 项目和解决方案
  • 构建并运行
  • Maximum number of parallel project builds

  • 还有另一种选择, /MP ,我可以通过以下方式在 GUI 中访问:
  • 项目
  • 房产
  • 配置属性
  • C/C++
  • 一般
  • Multi-processor Compilation

  • 如果我错了,请纠正我,但这似乎表明该项目将构建多个 .cpp并行文件,但没有选项指定多少,除非我必须在文本框中手动设置它(即: /MP 4/MP4

    似乎为了 /MP要工作,我需要禁用最小重建(即: Enabled Minimal Rebuild: No (/GM-) ),它也不适用于预编译的头文件。我通过使预编译头文件成为解决方案首先构建的专用项目,在所有其他项目之前解决了预编译头文件问题。

    对于这个问题:如何在解决方案中为并行项目构建实现这些选项,并在项目中构建并行文件 ( .cpp ),通过命令行使用 MSBuild,并确认它们按预期工作?我上面的假设是否也正确? 我的最终目标是测试使用最多使用的内核总数构建项目,或者如果我的构建过程受 I/O 限制而不是 CPU 限制,甚至可能使其重载。 我可以通过以下方式在 Linux 中轻松做到这一点:
    NUMCPUS=`grep -c '^processor' /proc/cpuinfo`
    NUMJOBS="$(echo "$NUMCPUS*1.5" | bc)"
    NUMJOBS="$(printf '%.0f' $NUMJOBS)"
    alias pmake='time nice make -j$NUMJOBS'

    这允许我在我的 8 核 PC 上一次排队 12 个构建任务,并且 make自动构建多达 12 个 .cpp并行文件,处理隐式构建规则/依赖项等。我试图在 Visual Studio Professional 和 MSBuild 中实现相同的目标。当我的构建受 I/O 限制时,这种方法最能将所有内核保持在 ~100%。用法。

    谢谢你。

    最佳答案

    /MP编译器 ( cl.exe ) 和 /m 的开关msbuild.exe 的选项确实是要走的路。这里有一些相关的documentation提取物:

    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. Optional Argument: The maximum number of processes that the compiler can create. If you omit the processMax argument, the compiler retrieves the number of effective processors on your computer from the operating system, and creates a process for each processor. The /MP option is incompatible with some compiler options and language features. If you use an incompatible compiler option with the /MP option, the compiler issues warning D9030 and ignores the /MP option

    Examples suppose you specify the following command line:

    cl /MP7 a.cpp b.cpp c.cpp d.cpp e.cpp

    In this case the compiler uses five processes because that is the lesser of five source files and a maximum of seven processes. Alternatively, suppose your computer has two effective processors and you specify the following command line:

    cl /MP a.cpp b.cpp c.cpp

    In this case the operating system reports two processors; therefore, the compiler uses two processes in its calculation. As a result, the compiler will execute the build with two processes because that is the lesser of two processes and three source files.



    所以你注意到这都是关于编译器的,还没有关于 msbuild 的消息,因为编译器是一个独立的工具。换句话说:假设您打开 2 个命令窗口,并在两个命令窗口中调用类似 cl /MP3 a.cpp b.cpp c.cpp 的命令。同时,您将同时运行 6 个编译器进程。这是您所追求的行为,也是 msbuild 可以做的事情。

    调用 msbuild /? 的输出:

    /maxcpucount[:n] Specifies the maximum number of concurrent processes to build with. If the switch is not used, the default value used is 1. If the switch is used without a value MSBuild will use up to the number of processors on the computer. (Short form: /m[:n])



    还有更多相关信息 here ,特别是它说的部分

    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.



    现在,如果您只需调用 msbuild /m my.vcxproj在一个典型的项目上,这不会做任何特别的事情,因为只有一个项目要构建。但是,如果在 msbuild 文件中调用了 MsBuild 任务,例如
    <MsBuild Projects="@(ListOfProjects)" BuildInParallel="True"/>

    并且/m 选项被传递给 msbuild 然后 msbuild 将根据上面列出的规则生成多个其他 msbuild 进程。巧合的是,这正是使用 msbuild 构建解决方案时发生的情况。 MSbuild 优先 converts the solution到一个实际的 msbuild 文件,该文件使用解决方案中的项目列表调用 MsBuild 任务。因此,可以并行构建多个项目,并为此创建多个 msbuild 进程。反过来,如果这些是带有/MP 选项集的 C++ 项目,那么这些项目中的每一个都将创建多个编译器进程,最多产生 <max num cl processes> * <max num msbuild instances>并行编译。这取决于您的构建机器什么是最佳的,如果您希望您可以使用这些选项(为了有效地做到这一点,您的每个项目都将导入一个公共(public)属性表,您可以在其中设置/MP 选项,否则您必须编辑所有这些都是单独的)并查看构建完成时 msbuild 报告的耗时。我总是将/m 和/MP 的参数保留为默认值,因为我不希望使用我的项目的其他开发人员有可能的次优配置,并且因为它很容易最大化 CPU。

    关于visual-studio - MSBuild - 如何并行构建多个文件和项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33787023/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com