visual-studio - 如何使用 msbuild 禁用预编译头?

标签 visual-studio msbuild precompiled-headers

问题

  • 我有一个使用预编译 header 的 VS2015 解决方案。
  • 我们的持续集成服务器使用 msbuild 构建解决方案。
  • 我们还使用了一个覆盖工具,该工具的预编译 header 存在一些问题。

问题

如何告诉 msbuild.exe 不要使用预编译头?

注意

我尝试在 msbuild 调用中添加 /Y-,但它不是 msbuild 的有效参数。

最佳答案

We can't directly do this by msbuild since PrecompiledHeader is not a msbuild property...

MSbuild 从项目文件中读取数据来执行构建。并且 xx.vcxproj 中有几个元素可以在命令行中指定,例如 targets , properties ,因此我们可以使用像 msbuild xx.vcxproj/xx.sln/t:xx/p:xx 这样的命令来在命令中构建时指定 target 元素和 property 元素 -线。

例如:msbuild xx.vcxproj/t:build/p:OutDir=xxx可以指定程序集的输出位置。

我换了一个VS2015 C++ project's settings为了使其不使用预编译头,在比较了我发现的两个版本的 xx.vcxproj 文件之间的差异(使用 git 进行版本控制)之后:

enter image description here

因此控制预编译头行为的元素是 Item metadataClCompile Item 。由于该元素是 Item 元数据 而不是 property,因此我们不能使用诸如 msbuild xx.vcxproj/p 之类的命令: PrecompiledHeader=NotUsing ... 禁用预编译头。 (/p <=> /property)

可能的方向:

我能想到的唯一可能实现您目标的方向是创建一个自定义属性来控制此行为。

1.对于一个普通的C++项目,其项目文件内容为:

<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <ClCompile>
      <PrecompiledHeader>NotUsing</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup>

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <ClCompile>
      <PrecompiledHeader>Use</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup>

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <ClCompile>
      <PrecompiledHeader>Use</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup>

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <ClCompile>
      <PrecompiledHeader>Use</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup> 

2.添加自定义 DisablePCH 属性,复制这四个 ItemDefinitionGroups 并将该属性纳入其 Condition 中。 .

<PropertyGroup>
    <DisablePCH>false</DisablePCH>
  </PropertyGroup>

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32' And '$(DisablePCH)'=='false'">
    <ClCompile>
      <PrecompiledHeader>Use</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup>

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64' And '$(DisablePCH)'=='false'">
    <ClCompile>
      <PrecompiledHeader>Use</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup>

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32' And '$(DisablePCH)'=='false'">
    <ClCompile>
      <PrecompiledHeader>Use</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup>

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64' And '$(DisablePCH)'=='false'">
    <ClCompile>
      <PrecompiledHeader>Use</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup>

<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32' And '$(DisablePCH)'=='true'">
    <ClCompile>
      <PrecompiledHeader>NotUsing</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup>

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64' And '$(DisablePCH)'=='true'">
    <ClCompile>
      <PrecompiledHeader>NotUsing</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup>

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32' And '$(DisablePCH)'=='true'">
    <ClCompile>
      <PrecompiledHeader>NotUsing</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup>

  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64' And '$(DisablePCH)'=='true'">
    <ClCompile>
      <PrecompiledHeader>NotUsing</PrecompiledHeader>
      ...
    </ClCompile>
  </ItemDefinitionGroup>

3.默认情况下,如果我们使用像 msbuild xx.vcxproj ... 这样的普通命令,它将使用预编译头。如果我们想在命令中禁用它,请使用类似 msbuild xx.vcxproj/p:DisablePCH=true ... 的命令。

ps:如果你确实想通过msbuild禁用PCH(预编译头),你可以尝试可能的方向,由于某种原因我无法在我的机器上测试它:(。请随时告诉我是否有帮助。

关于visual-studio - 如何使用 msbuild 禁用预编译头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58494965/

相关文章:

visual-studio - 错误消息 "No exports were found that match the constraint contract name"

android - 如何测量 Android Xamarin 应用程序的数据使用情况?

c# - "AssemblyInfo"MSBuild 任务在 Jenkins 构建中失败

msbuild - MSBuild 社区任务或扩展包是否适用于 Mono 的 xbuild?

msbuild - 如何仅在存在时终止任务 msbuild

c# - 使用不同的配置调试同一项目的 2 个以上实例

c# - LINQ 的问题 - 有必要添加对不需要的库的引用

precompiled-headers - 用于 xcode 的 Gyp 预编译 header

c++ - 包含标准库时不使用预编译头文件

c++ - 在我的类头文件中的预编译头文件中包含头文件并在其外部包含头文件