visual-studio - 如何在 Visual Studio 2017 和 Visual Studio 2019 之间为每个项目引用 vcxproj.filters 和 vcxproj.user 的一份副本

标签 visual-studio visual-c++ visual-studio-2017 visual-studio-2019

我有一个在 Visual Studio 2017 社区版中创建的 C++ 项目,我已使用 Visual Studio 2019 社区版打开并“转换”它。 项目文件夹包含:

Main.sln
Main/Main.vcxproj
Main/Main.vcxproj.filters
Main/Main.vcxproj.user

根据 Winmerge,转换仅影响 Main/Main.vcxproj 中的两个值:

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
...
<PlatformToolset>v141</PlatformToolset>

更改为

<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
...
<PlatformToolset>v142</PlatformToolset>

有哪些选项可以为 VS2017 和 VS2019 维护大部分或全部项目文件的一份副本?

另外,我想知道:

  • .vcxproj 或 .sln 中是否支持条件?
  • 有没有一种方法可以显式指向一个文件夹路径中的 .vcxproj 并 *.vcxproj.filters 和 *.vcxproj.user 在另一个中?

谢谢!

最佳答案

希望这会对以后的人有所帮助。

MSBuild supports conditionals defined within the .vcxproj files可以关闭可用的宏。我还发现可以通过在文件中进一步设置来替换值。

This example讨论检查和设置 VisualStudioVersion 宏。

我找到的解决方案使用 DefaultPlatformToolset 宏,对于 VS2017 为 v141,对于 VS2019 为 v142。

Main.vcxproj 可以通过两种方式在条件中使用它:

1) 在包含必要值的 PropertyGroup 周围使用 Choose、When 和 Another 标签:

<Choose>
  <When Condition="'$(DefaultPlatformToolset)'=='v141'">
    <PropertyGroup Label="Globals">
      <WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
    </PropertyGroup>
    <PropertyGroup Label="Configuration">
      <PlatformToolset>v141</PlatformToolset>
    </PropertyGroup>
  </When>
  <When Condition="'$(DefaultPlatformToolset)'=='v142'">
    <PropertyGroup Label="Globals">
      <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
    </PropertyGroup>
    <PropertyGroup Label="Configuration">
      <PlatformToolset>v142</PlatformToolset>
    </PropertyGroup>
  </When>
  <Otherwise>
    <PropertyGroup Label="Globals">
      <WindowsTargetPlatformVersion>$(DefaultWindowsSDKVersion)</WindowsTargetPlatformVersion>
    </PropertyGroup>
    <PropertyGroup Label="Configuration">
      <PlatformToolset>$(DefaultPlatformToolset)</PlatformToolset>
    </PropertyGroup>
  </Otherwise>
</Choose>

2)设置PropertyGroups的Conditional属性:

<PropertyGroup Label="Globals">
  <WindowsTargetPlatformVersion>$(DefaultWindowsSDKVersion)</WindowsTargetPlatformVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(DefaultPlatformToolset)'=='v141'" Label="Globals">
  <WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(DefaultPlatformToolset)'=='v142'" Label="Globals">
  <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>

另一种方法可能是使用每个 Visual Studio 版本的 .sln 文件并在 $(SolutionFileName) 宏上使用基本条件。

<Choose>
  <When Condition="'$(SolutionFileName)'=='Main_VS2017.sln'">
    ...
  </When>
  <When Condition="'$(SolutionFileName)'=='Main_VS2019.sln'">
    ...
  </When>
</Choose>

关于visual-studio - 如何在 Visual Studio 2017 和 Visual Studio 2019 之间为每个项目引用 vcxproj.filters 和 vcxproj.user 的一份副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58920244/

相关文章:

c# - 如何在 Visual Studio 2017 中保持输出窗口始终打开

vb.net - 如何在安装项目中添加许可协议(protocol)

c++ - 为什么使用这个库需要它的内部头文件?

c++ - FireBreath 将值从一个函数传递到另一个函数并在 JavaScript 中使用该函数

c++ - 如何使用草蛉项目

visual-studio-2017 - Visual Studio 2017多个 "Keep Target Branch Version"

c# - Xamarin - 布局云未加载 : The operation failed due to an internal error

c++ - boost - 添加到 'corecrt.h' 的路径

json - Visual Studio 对 JSON 属性排序错误

python - Premake 在我的存储库中找不到 lua 脚本(完全相同的存储库在我的 mainpc 上工作)