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

5
我的老师被学校强制使用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 ==========

3
你不觉得“但它仍会产生错误”并不是大多数人会认为有用的错误描述吗? - Timbo
@Timbo 好主意,我会运行一个并列出错误。 - Griffin Melson
@Timbo 添加控制台输出。如果有其他任何信息需求,请问。 - Griffin Melson
1
可能相关 - NathanOliver
@NathanOliver 是的,那是我尝试过的解决方案。 - Griffin Melson
1
马 = 强行 ? :-) - TarmoPikaro
3个回答

8

已更新至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中转到DebugRelease设置并选择<inherit from parent or project defaults>时,您将默认获得2015版本而非2010版本。

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

  1. 将ToolsVersion属性设置为4.0
  2. 为Visual Studio 2015添加PlatformToolset的通用默认值v140
  3. 为Visual Studio 2017添加PlatformToolset的通用默认值v141
  4. 为Visual Studio 2019添加PlatformToolset的通用默认值v142
  5. 为Visual Studio 2022添加PlatformToolset的通用默认值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>
      ...

只需将 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的行,以便继续在所有四个版本之间无缝跨载项目。


4
问题在于项目文件引用了 v140 C++ 工具集,这基本上意味着“使用 Visual Studio 2015 的 C++ 编译器”。由于该编译器没有安装,所以导致出现错误消息。
从我的经验中,有两种方法可以解决您的问题:
  • 在您的计算机上安装 Visual Studio 2010。然后,在 Visual Studio 2015 中选择 2010 平台工具集作为项目设置。这样,您的项目将始终使用 2010 进行编译,但您不会意外使用 2010 没有的 C++ 功能。

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

这两种解决方案本质上意味着您不会使用 Visual Studio 2015 相对于 Visual Studio 2010 的改进的 C++ 能力,这有些不幸。

在我老师的电脑上安装更新的工具集是否可行? - Griffin Melson
@GriffinMelson 我不确定它是否(或将会)作为单独的下载可用。 - Timbo

0

编写一个 .lua 脚本用于 premake5 - https://premake.github.io/ 如何使用可以在这里找到:https://github.com/premake/premake-core/wiki

然后只需使用命令行中的特定 Visual Studio 版本为特定的 Visual Studio 创建项目 - 例如像这样:

premake5 --file=myproject.lua vs2015
premake5 --file=myproject.lua vs2010

典型的脚本看起来像这样:

-- If visual studio version is not specified from command line - use vs2013
if _ACTION == nil then
    _ACTION = "vs2013"
end

buildvsver = _ACTION

--
-- I typically use "_vs2013" suffix so autogenerated projects won't conflict with each other.
--
solution ( "MyOwnSolution" .. "_" .. buildvsver)
    platforms {  "x32", "x64" }
    configurations { "Debug", "Release" }
    objdir (  "obj/" .. buildvsver)

project ("MyOwnProject" .. "_" .. buildvsver)
    kind     "SharedLib"                -- http://industriousone.com/kind: ConsoleApp | SharedLib | StaticLib | WindowedApp
    platforms {  "x32", "x64" }
    language "C++"
    targetdir ("bin/%{cfg.buildcfg}_%{cfg.platform}_" .. buildvsver)

    -- If you use managed code
    flags { "Managed" }

    flags { "MFC" }
    flags { "Unicode" }

    -- Add dependency on another project:
    -- dependson { "OtherProject" .. "_" .. buildvsver }

    -- If you use managed code - you can specify .net framework version.
    framework "4.0"

    files {
        "mysource1.cpp",
        "myheader1.h",
        "myheader2.cpp",
    }

    links {
        -- Some of dependent libraries
        "dbghelp.lib",
        "delayimp.lib"
    }

    -- Force to delay load some .dll
    -- Custom / advanced flags.
    linkoptions { "/delayload:dbghelp.dll " }
    linkoptions { "/delayload:mscoree.dll " }

    configuration "*"
        -- I typically use 'ReleaseRuntime' - that's debug = release configuration. 
        -- No special .dll's are needed even for debug version of your application
        flags { "NoRuntimeChecks", "ReleaseRuntime" }

        -- Debug symbols.
        flags { "Symbols" }

        -- Executable name without _vs2013 prefix.
        targetname ( "MyOwnProject" )

        -- C++ defines for both - release and debug configurations.
        defines { "NDEBUG", "_CRT_SECURE_NO_WARNINGS", "WIN32", "WINVER=0x0600", "_WIN32_WINNT=0x0600" }

        -- debugcommand "customExeToLaunch.exe"

        -- Custom post build steps.
        -- postbuildcommands { "call $(ProjectDir)projexport.bat $(PlatformName) $(TargetPath)" }

    configuration "Release"
        -- Only difference from debug - is optimizations for speed.
        optimize "Speed"

        -- Can debug in release.

        --
        -- Enhance Optimized Debugging
        -- https://randomascii.wordpress.com/2013/09/11/debugging-optimized-codenew-in-visual-studio-2012/
        -- https://msdn.microsoft.com/en-us/library/dn785163.aspx
        --
        buildoptions { "/Zo" }


project ("TestMyProject" .. "_" .. buildvsver)
    platforms {  "x32", "x64" }
    kind     "ConsoleApp"
    language "C#"
    targetdir ("bin/%{cfg.buildcfg}_%{cfg.platform}_" .. buildvsver)
    framework "4.0"

    links {
        "System",
        "System.Core",
        "System.Data",
        "System.Drawing",
        "System.Windows.Forms",
        "System.Xml",
        "MyOwnProject" .. "_" .. buildvsver
    }

    files    { 
        "TestMyProject.cs",
    }

    configuration "*"
        targetname ( "TestMyProject" )
        flags { "Symbols" }
        defines { "DEBUG" }

在你理解了事物的运作方式之后,你甚至可以为.lua本身创建自定义构建步骤来启动premake5,或者定制项目生成 - 比如创建帮助你更高级项目的lua函数。

请注意,我使用了很多你可能不需要的高级内容(我的大多数项目都是编译为64位和32位CPU等等...)- 也许从零开始比复制我展示的配置更有意义。然后你就会理解事物的运作方式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接