已更新至Visual Studio 2017、2019和2022
如果您只使用Visual Studio IDE本身(而非命令行上的MSBuild)进行编译,那么只需进行一些更改,即可在两个平台上实现更多或更少完整的功能。
不幸的是,C++项目的规则与C#/.NET不同,并且需要一些手动干预,不像C#项目那样在“升级”后相对自动化。这些更改需要手动编辑项目文件。
当通过IDE运行构建时,Visual Studio的较新版本将覆盖工具版本。只需将ToolsVersion
设置为4.0以满足Visual Studio 2010,然后在通用属性组中修复PlatformToolset
,以获得Visual Studio 2015 IDE中的正确默认操作,可能就可以了。
设置PlatformToolset
的原因是,当更改构建属性时,例如在IDE中转到Debug
或Release
设置并选择<inherit from parent or project defaults>
时,您将默认获得2015版本而非2010版本。
C++项目文件与Visual Studio 2010、2015、2017、2019和2022共存的步骤:
- 将ToolsVersion属性设置为4.0
- 为Visual Studio 2015添加PlatformToolset的通用默认值v140
- 为Visual Studio 2017添加PlatformToolset的通用默认值v141
- 为Visual Studio 2019添加PlatformToolset的通用默认值v142
- 为Visual Studio 2022添加PlatformToolset的通用默认值v143
- 保存文件并重新加载项目
1. 将工具版本设置为4.0:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
...
只需将 Project
标签中的 ToolsVersion
的值从 14.0
更改为 4.0
,即可实现该功能。
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
...
2. 将Visual Studio 2015识别的v140平台工具集的常见默认值添加到中:
<PropertyGroup Label="Globals">
<ProjectGuid>{12345678-9876-ABCD-DCCA-765FE987AA1F}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>myProject</RootNamespace>
<TargetPlatformVersion>8.1</TargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
通过仅在
PropertyGroup
底部添加新的
PlatformToolset
行,它变成了:
<PropertyGroup Label="Globals">
<ProjectGuid>{12345678-9876-ABCD-DCCA-765FE987AA1F}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>myProject</RootNamespace>
<TargetPlatformVersion>8.1</TargetPlatformVersion>
<PlatformToolset Condition="'$(VisualStudioVersion)' == '14.0'">v140</PlatformToolset>
<PlatformToolset Condition="'$(VisualStudioVersion)' == '15.0'">v141</PlatformToolset>
<PlatformToolset Condition="'$(VisualStudioVersion)' == '16.0'">v142</PlatformToolset>
<PlatformToolset Condition="'$(VisualStudioVersion)' == '17.0'">v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
为了在Visual Studio 2017中加载,需要像上面所示添加带有工具集v141
的行,以便继续在所有三个版本之间无缝跨载项目。
在Visual Studio 2019中,需要像上面所示添加带有工具集v142
的行,以便继续在所有四个版本之间无缝跨载项目。