c++ - 如何使 Visual Studio 2015 C++ 项目与 Visual Studio 2010 兼容?

标签 c++ visual-studio-2010 visual-studio visual-studio-2015

学校要求我的老师使用 Visual Studio 2010,因为他们不想安装任何新东西。我一直在使用 Visual Studio 2015,我非常喜欢它。然而,当她尝试运行任何代码时,它会产生一堆错误。我尝试了一种通过编辑解决方案文件使 2013/2012 项目与 2010 兼容的解决方案,但它仍然会产生错误。有解决办法吗?

这是我尝试在 Visual Studio 2010 中运行源文件时的控制台输出:

1>------ Build started: Project: typingSalon, Configuration: Debug Win32 ------
1>Build started 4/8/2015 8:19:30 AM.
1>Project file contains ToolsVersion="14.0". This toolset is unknown or missing. You may be able to resolve this by installing the appropriate .NET Framework for this toolset. Treating the project as if it had ToolsVersion="4.0".
1>C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\Microsoft.Cpp.Win32.Targets(518,5): error MSB8008: Specified platform toolset (v140) is not installed or invalid. Please make sure that a supported PlatformToolset value is selected.
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.05
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

针对 Visual Studio 2017、2019 和 2022 进行了更新

如果您只使用 Visual Studio IDE 本身(而不是命令行上的 MSBuild)进行编译,那么您只需进行一些更改,就可以使这项工作真正起作用,并且在两个平台上都具有或多或少的全部功能。

不幸的是,C++ 项目的规则与 C#/.NET 不同,需要一些手动干预,这与 C# 项目不同,C# 项目在项目“升级”后相当自动地进行往返。这些更改需要手动编辑项目文件。

当通过 IDE 运行构建时,更高版本的 Visual Studio 将覆盖工具版本。只需将 ToolsVersion 设置为 4.0 即可满足 Visual Studio 2010 的要求,然后在公共(public)属性组中修复 PlatformToolset 以在 Visual Studio 2015 IDE 中获得正确的默认操作。

设置 PlatformToolset 的原因是为了在更改构建属性时正确设置默认值,例如当您转到 IDE 中的 DebugRelease 设置并选择 <inherit from parent or project defaults> 时,默认情况下您将获得 2015 版本而不是 2010。

C++项目文件与Visual Studio 2010、2015、2017、2019、2022共存的步骤:

  1. ToolsVersion 属性为 4.0
  2. 将 PlatformToolset 的通用默认值添加到 v140 for Visual Studio 2015
  3. 将 PlatformToolset 的通用默认值添加到 v141 for Visual Studio 2017
  4. 将 PlatformToolset 的通用默认值添加到 v142 for Visual Studio 2019
  5. 将 PlatformToolset 的通用默认值添加到 v143 for Visual Studio 2022
  6. 保存文件并重新加载项目

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

14.04.0 标签中仅将 Project 更改为 ToolsVersion 就变成了

<?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。将 PlatformToolset 的通用默认值添加到 v140 只有 Visual Studio 2015 才能识别:

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

通过仅将新的 PlatformToolset 行添加到 PropertyGroup 的底部,它变成:

  <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 的代码,以继续在所有四个项目之间无缝交叉加载项目。

关于c++ - 如何使 Visual Studio 2015 C++ 项目与 Visual Studio 2010 兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29515566/

相关文章:

.net - 我可以在断点/失败的测试中导致 NUnit GUI 进入 Visual Studio 调试器吗?

c++ - 由于 Unresolved external 问题,OpenCV 无法编译 -- LNK2019

c# - 调试时如何在内存中搜索字符串?

visual-studio-2010 - 如何在 Visual C++ 2010 中添加要链接的 lib 文件?

visual-studio - 如何在 VS 2019 状态栏中禁用 Resharper 消息?

c++ - __declspec(dllimport) 实际上是如何工作的?

C++ 处理 ASCII 中的数字以仅保留在字母范围内

Android JNI ndk-构建错误 : 'jni' has not been declared

c++ - g++ libstdc++.so.6 : version `CXXABI_1.3.9' not found after upgrade to gcc version 7. 3.0 从 4.8.5

c# - 命名空间错误 OfficeOpenXML EPPlus