我有一个包含 10 多个 C++ 项目的大型解决方案。
整个解决方案只有 x64,除了项目 P 需要 x64 和 win32 版本(正确的在运行时加载)。
项目 P 依赖于其他几个项目的 lib 文件:C 和 H,它们被编译成 libs。
P 引用了 C 和 H,如下所示:
<ProjectReference Include="..\C\C.vcxproj">
<Project>{....}</Project>
</ProjectReference>
<ProjectReference Include="..\H\H.vcxproj">
<Project>{....}</Project>
</ProjectReference>
我想为两个平台构建项目P。
我选择从一个 meta-P 项目中执行此操作,它像这样调用 P:
<MSBuild Projects="..\P\P.vcxproj" Properties="Platform=Win32"/>
<MSBuild Projects="..\P\P.vcxproj" Properties="Platform=x64"/>
这允许开发人员自由更改 P,然后通过构建 meta-P 同时构建两个版本。
问题是当 meta-P 在项目 P 上调用 MSBuild 时,对 C 和 H 的引用受解决方案环境影响(其中事件平台始终为 x64)。
当谈到将 Win32 P 链接到其适当的 C.lib 和 H.lib 时,开放解决方案配置开始, studio 尝试将其与 x64 版本链接,但失败了。
我暂时解决了它,使用 meta-P 中的 exec 任务直接在 P 上运行 MsBuild.exe>。这忽略了 Visual Studio 环境属性。
让平台正确读取的正确解决方案是什么?
最佳答案
正确的解决方案是添加名为 ShouldUnsetParentConfigurationAndPlatform 的未记录属性并将其设置为 false。
放置它的唯一正确位置是在 P 项目的每个 Project Reference 项中,否则它会被忽略。
所以它最终看起来像这样:
<ProjectReference Include="..\C\C.vcxproj">
<Project>{....}</Project>
<Properties>ShouldUnsetParentConfigurationAndPlatform=false</Properties>
</ProjectReference>
<ProjectReference Include="..\H\H.vcxproj">
<Project>{....}</Project>
<Properties>ShouldUnsetParentConfigurationAndPlatform=false</Properties>
</ProjectReference>
这会导致 Visual Studio 继续使用 C 和 H 的继承属性,而不是从解决方案环境中读取它们。
编辑:从评论中查看有用的引用: https://github.com/Microsoft/msbuild/blob/98d38cb/src/XMakeTasks/AssignProjectConfiguration.cs#L198-L218
关于c++ - 让 MSBuild 编译具有 Win32 和 Win64 平台依赖项的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25137585/