如何在 visual studio 2017 和 visual studio 2019 之间为每个项目引用一份 vcxproj.filters 和 vcxproj.user

How to reference one copy of vcxproj.filters and vcxproj.user per project between visual studio 2017 and visual studio 2019

我有一个在 Visual Studio 2017 社区版中创建的 c++ 项目,我已经用 Visual Studio 2019 社区版打开并 "converted" 它。 项目文件夹包含:

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 维护大部分或所有这些项目文件的一份副本?

另外,我想知道:


谢谢!

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

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

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

我找到的解决方案使用了 DefaultPlatformToolset 宏,它是 VS2017 的 v141 和 VS2019 的 v142。

有两种方法 Main.vcxproj 可以在有条件的情况下使用它:

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

<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 的条件 属性:

<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>

我已经使用了 http://www.markusweimer.com/2016/03/14/visual-c++/ 的解决方案,并将其转换为 2015 和 2019。

<!--
    Switch the PlatformToolset based on the Visual Studio Version
-->
<PropertyGroup>
    <!-- Assume Visual Studio 2015 / 14.0 as the default -->
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
</PropertyGroup>
<!-- Visual Studio 2019 (16.0) -->
<PropertyGroup Condition="'$(VisualStudioVersion)' == '16.0'">
    <ConfigurationType>DynamicLibrary</ConfigurationType>
    <UseOfMfc>false</UseOfMfc>
    <CharacterSet>MultiByte</CharacterSet>
    <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<!-- Visual Studio 2015 (14.0) -->
<PropertyGroup Condition="'$(VisualStudioVersion)' == '14.0'">
    <ConfigurationType>DynamicLibrary</ConfigurationType>
    <UseOfMfc>false</UseOfMfc>
    <CharacterSet>MultiByte</CharacterSet>
    <PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<!--
    End of: Switch the PlatformToolset based on the Visual Studio Version
-->

很有魅力...