簡體   English   中英

如何使Visual Studio 2015 C ++項目與Visual Studio 2010兼容?

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

我的老師很想在學校使用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和Visual Studio 2019進行了更新

如果您只使用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 。

Visual Studio 2010,Visual Studio 2015,Visual Studio 2017和Visual Studio 2019同步在C ++的同一項目文件中的步驟:

  1. ToolsVersion屬性為4.0
  2. 對於Visual Studio 2015,將PlatformToolset的常用默認值添加到v140
  3. 對於Visual Studio 2017,將PlatformToolset的常用默認值添加到v141
  4. 對於Visual Studio 2019,將PlatformToolset的常用默認值添加到v142
  5. 保存文件並重新加載項目

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

通過在ToolsVersion Project標簽中僅將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.將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>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />

要在Visual Studio 2017中加載,還需要一個包含工具集v141的行,如上所示,以繼續在這三者之間無縫地交叉加載項目。

在Visual Studio 2019中,如上所示,還需要一個包含工具集v142的行,以繼續在所有四個項目之間無縫地交叉加載項目。

問題是項目文件引用了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的改進C ++功能而不是Visual Studio 2010,這有點不幸。

為premake5寫一個.lua腳本 - 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,等等......) - 從零開始可能是有意義的復制配置由我顯示。 然后你就會明白事情是如何運作的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM