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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29515566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:11:57  来源:igfitidea点击:

How do I make a Visual Studio 2015 C++ project compatible with Visual Studio 2010?

c++visual-studio-2010visual-studiovisual-studio-2015

提问by Griffin Melson

My teacher is horsed to use Visual Studio 2010 by the school, because they don't want to bother installing anything new. I've been using Visual Studio 2015 and am really liking it. However, when she tries to run any of the code, it produces a bunch of errors. I tried a solution for making 2013/2012 projects compatible with 2010 by editing the solution file, but it still produces errors. Is there a solution?

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

Here is the console output when I try to run the source file in Visual Studio 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 ==========

采纳答案by Timbo

The problem is that the project file references the v140C++ toolset, which basically means use the C++ compiler from Visual Studio 2015. This compiler is not installed, which causes your error message.

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

From the top of my head, there are two ways for you to overcome your situation:

在我的脑海里,有两种方法可以让你克服你的处境:

  • Install Visual Studio 2010 on your computer. Then, from within 2015, select the 2010 platform toolset in the project settings. Your project will then always be compiled with 2010, but you have the advantage to not accidentally use C++ features that 2010 does not have.

  • Don't install Visual Studio 2010 on your computer, but use the second computer (with just 2010 installed) to create a second build configuration, which has the platform toolset set to Visual Studio 2010 (v100). Use the appropriate configuration depending on which Visual Studio you use.

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

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

Both of these solutions basically mean you do not use the improved C++ capabilities of Visual Studio 2015 over Visual Studio 2010, which is somewhat unfortunate.

这两种解决方案基本上意味着您没有使用 Visual Studio 2015 改进的 C++ 功能而不是 Visual Studio 2010,这有点不幸。

回答by Celess

Updated for Visual Studio 2017 and Visual Studio 2019

为 Visual Studio 2017 和 Visual Studio 2019 更新

You can make this work actually, with just a few changes, if you only use the Visual Studio IDE itself (not MSBuild on the command line) to compile, with more or less full functionality on both platforms.

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

Unfortunately the rules for C++ projects are different than C#/.NET, and require some manual intervention, unlike the C# projects fairly automatic for round tripping after project "upgrade". These changes will require editing the project files manually.

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

Later versions of Visual Studio will override the tools version when the build is run through the IDE. Simply setting the ToolsVersionto 4.0, to satisfy Visual Studio 2010 and then fixing the PlatformToolsetin a common property group to get the correct default action in the Visual Studio 2015 IDE will probably do it.

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

The reason for setting PlatformToolsetis so that the defaults for correctly when changing build properties, like when you go to Debugor Releasesettings in the IDE and choose <inherit from parent or project defaults>you will get the 2015 version by default and not 2010.

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

Steps for Visual Studio 2010, Visual Studio 2015, Visual Studio 2017, and Visual Studio 2019 cohabitation on same project file for C++:

Visual Studio 2010、Visual Studio 2015、Visual Studio 2017 和 Visual Studio 2019 在 C++ 的同一项目文件上共存的步骤:

  1. ToolsVersion attribute to 4.0
  2. Add common default value of PlatformToolset to v140 for Visual Studio 2015
  3. Add common default value of PlatformToolset to v141 for Visual Studio 2017
  4. Add common default value of PlatformToolset to v142 for Visual Studio 2019
  5. Save file and reload project
  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. 保存文件并重新加载项目

1. Tools version to 4.0:

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

By changing only 14.0to 4.0in Projecttag for ToolsVersionit becomes

通过只更改14.04.0inProject标签,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. Add common default value of PlatformToolset to v140 recognized only by Visual Studio 2015:

2.在v140中添加PlatformToolset通用默认值,只有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" />

By adding only the new PlatformToolsetline to the bottom of the PropertyGroupit becomes:

通过仅在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>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />

To also load in Visual Studio 2017, a line with toolset v141is additional required as shown above to continue to seamlessly cross load projects between all three.

要在 Visual Studio 2017 中加载,v141还需要额外的工具集,如上所示,以继续在所有三个之间无缝交叉加载项目。

In Visual Studio 2019, a line with toolset v142is additional required as shown above to continue to seamlessly cross load projects between all four.

在 Visual Studio 2019 中,v142需要额外的工具集,如上所示,以继续在所有四个之间无缝交叉加载项目。

回答by TarmoPikaro

Write a .lua script for premake5 - https://premake.github.io/How to can be found here: https://github.com/premake/premake-core/wiki

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

And then simply create project for specific visual studio using visual studio version from command line - for example like this:

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

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

Typical script looks like this:

典型的脚本如下所示:

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

After you reach some sort of understanding how things works - you can even create for .lua itself it's own custom build step to launch premake5, or even to customize project generation - like create lua functions which helps you more advanced projects.

在您对事情的工作原理有所了解后 - 您甚至可以为 .lua 本身创建它自己的自定义构建步骤来启动 premake5,或者甚至自定义项目生成 - 例如创建 lua 函数来帮助您进行更高级的项目。

Please note that I'm using a lot of advanced stuff which you might not need (Most of my projects are compiling for 64 and 32 bit cpu's, and so on...) - may be it makes sense to start from zero than to copy config shown by me. Then you will have understanding how things works.

请注意,我正在使用许多您可能不需要的高级内容(我的大多数项目都在为 64 位和 32 位 cpu 编译,等等......) - 从零开始可能比从零开始更有意义复制我显示的配置。然后你就会明白事情是如何运作的。