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

新手上路,请多包涵

我的老师被学校推荐使用 Visual Studio 2010,因为他们不想费心安装任何新东西。我一直在使用 Visual Studio 2015,并且非常喜欢它。但是,当她尝试运行任何代码时,会产生一堆错误。我尝试了通过编辑解决方案文件使 20132012 项目与 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 ==========

原文由 Griffin Melson 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

问题是项目文件引用了 v140 C++ 工具集,这基本上意味着 _使用 Visual Studio 2015 中的 C++ 编译器_。未安装此编译器,这会导致您的错误消息。

在我看来,有两种方法可以让你克服你的情况:

  • 在您的计算机上安装 Visual Studio 2010。然后,从 2015 年开始,在项目设置中选择 2010 平台工具集。您的项目将始终使用 2010 进行编译,但您的优势是不会意外使用 2010 没有的 C++ 功能。

  • 不要在您的计算机上安装 Visual Studio 2010,而是使用第二台计算机(仅安装 2010)创建第二个构建配置,其中平台工具集设置为 Visual Studio 2010 (v100)。根据您使用的 Visual Studio 使用适当的配置。

这两种解决方案基本上意味着您不使用 Visual Studio 2015 相对于 Visual Studio 2010 改进的 C++ 功能,这有点遗憾。

原文由 Timbo 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

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

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

通过 IDE 运行构建时,更高版本的 Visual Studio 将覆盖工具版本。只需将 ToolsVersion 设置为 4.0,以满足 Visual Studio 2010,然后在通用属性组中修复 PlatformToolset 以在 Visual Studio 2015 IDE 中获得正确的默认操作,可能会做到这一点.

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

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

  1. ToolsVersion 属性为 4.0
  2. 将 PlatformToolset 的通用默认值添加到 v140 for Visual Studio 2015
  3. 将 PlatformToolset 的通用默认值添加到 Visual Studio 2017 的 v141
  4. 将 PlatformToolset 的通用默认值添加到 Visual Studio 2019 的 v142
  5. 将 PlatformToolset 的常用默认值添加到 Visual Studio 2022 的 v143
  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.0 更改为 4.0Project 标记为 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的通用默认值添加到仅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" />

通过仅将新的 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 的行,如上所示,以继续在所有四个之间无缝交叉加载项目。

原文由 Beeeaaar 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题