使用 Visual Studio 中的本机多目标来生成旧项目Use native multi-targeting in Visual Studio to build old projects

通常,建议在安装最新版本的 Visual Studio 时更新项目。Normally, we recommend that you update your projects when you install the latest version of Visual Studio. 更新项目和代码所要花费的成本通常会大过新 IDE、编译器、库和工具所带来的好处。The cost of updating your projects and code is usually more than offset by the benefits of the new IDE, compiler, libraries, and tools. 但是,我们知道你可能无法更新某些项目。However, we know that you may not be able to update some projects. 所拥有的二进制文件可能与那些因维护原因而无法升级的旧库或平台绑定在一起。You may have binaries that are tied to older libraries or platforms that for maintenance reasons you can’t upgrade. 你的代码可能使用非标准的语言构造,如果移动到更新的编译器,构造会发生中断。Your code may use non-standard language constructs that would break if you moved to a more recent compiler. 代码可能依赖于为特定 Visual C++ 版本编译的第三方库。Your code might rely on 3rd party libraries compiled for a specific version of Visual C++. 或者可能需要为某些组织生成库,而这些组织必须以某个 Visual C++ 的特定较旧版本为目标。Or you may produce libraries for others that must target a specific older version of Visual C++.

幸运的是,可以使用 Visual Studio 2017 和 Visual Studio 2015 生成面向较旧编译器工具集和库的项目。Fortunately, you can use Visual Studio 2017 and Visual Studio 2015 to build projects that target older compiler toolsets and libraries. 不必升级 Visual Studio 2010、Visual Studio 2012、Visual Studio 2013 或 Visual Studio 2015 项目即可利用 IDE 中的新功能:You don't have to upgrade a Visual Studio 2010, Visual Studio 2012, Visual Studio 2013, or Visual Studio 2015 project to take advantage of new features in the IDE:

  • 新的 C++ 重构功能和编辑器实验功能New C++ refactoring capabilities and editor experimental features
  • 新的诊断工具调试器窗口和错误列表窗口New Diagnostics tools debugger window and Error List window
  • 改进的断点、异常窗口和新的 PerfTipsRevamped breakpoints, exceptions window and new PerfTips
  • 新的代码导航和搜索工具New code navigation and search tools
  • 新的 C++ 快速修复和 Productivity Power Tools 扩展。New C++ Quick fixes and the Productivity Power Tools extensions.

还可以面向 Visual Studio 2008 项目,但不能一成不变地使用这些项目。You can also target Visual Studio 2008 projects, but they can't be used unchanged. 有关详细信息,请参阅 Visual Studio 2008 部分的说明。For details, see the Instructions for Visual Studio 2008 section.

最新版本的 Visual Studio 支持项目的本机多目标和往返。The latest versions of Visual Studio support native multi-targeting and round-tripping of projects. 本机多目标是一种功能,即最新的 IDE 使用 Visual Studio 旧版本安装的工具集进行生成。Native multi-targeting is the ability of the latest IDE to build using toolsets installed by previous versions of Visual Studio. 往返是一种功能,即最新的 IDE 可加载由 IDE 旧版本创建的项目,而无需对项目进行任何更改。Round-tripping is the ability of the latest IDE to load projects created by a previous IDE version without making any changes to the project. 如果并行安装最新版本和现有版本的 Visual Studio,则可以配合使用新版本的 IDE 与现有版本的编译器和工具,来生成项目。If you install the latest version of Visual Studio side-by-side with your existing version, you can use the new version of the IDE with the compiler and tools from the existing version to build your projects. 团队的其他成员可以继续在旧版本的 Visual Studio 中使用项目。Other members of your team can continue to use the projects in the older version of Visual Studio.

使用较旧的工具集时,可以利用许多最新的 IDE 功能,但不能利用 C++ 编译器、库和生成工具中的最新改进。When you use an older toolset, you can take advantage of many of the latest IDE features, but not the latest advances in the C++ compiler, libraries and build tools. 例如,将无法使用新的语言一致性改进、新调试和代码分析功能,或实现更快的最新工具集生成吞吐量。For example, you won't be able to use the new language conformance improvements, new debugging and code analysis features, or get the faster build throughput of the latest toolset. 也有一些 IDE 功能与旧的工具集不兼容。There are also some IDE features that are incompatible with older toolsets. 例如,内存探查器中可能缺少类型信息,重构操作转换为原始字符串会生成 C++11 兼容代码,该代码在用户使用 Visual Studio 2012 或更旧版本的工具集时不进行编译。For example, type information may be missing in the Memory Profiler, and the refactoring operation Convert to Raw string literals generates C++11-compliant code that won't compile when you use Visual Studio 2012 or older toolsets.

如何使用 Visual Studio 中的本机多目标How to use native multi-targeting in Visual Studio

并行安装 Visual Studio 和较旧版本后,在 Visual Studio 的新版本中打开现有项目。Once you have installed Visual Studio side-by-side with your older version, open your existing project in the new version of Visual Studio. 加载项目时,Visual Studio 会询问是否要对其进行升级以使用最新的 C++ 编译器和库。When the project is loaded, Visual Studio asks you whether you want to upgrade it to use the latest C++ compiler and libraries. 由于希望项目保留旧版编译器和库,因此选择“取消”按钮。Since you want the project to keep the older compiler and libraries, choose the Cancel button.

Visual Studio 会持续提示升级项目。Visual Studio is persistent about upgrading your project. 为避免在每次加载项目时看到升级对话框,可在项目中或在项目导入的 .props 或 .targets 文件中定义以下属性:To avoid seeing the upgrade dialog every time you load the project, you can define the following property in your projects, or in the .props or .targets files they import:

<VCProjectUpgraderObjectName>NoUpgrade</VCProjectUpgraderObjectName>

如果想升级项目,必须删除此属性。You must remove this property when you want to upgrade your projects.

如果选择不升级,Visual Studio 不会对解决方案或项目文件进行更改。If you choose not to upgrade, Visual Studio makes no changes to your solution or project files. 生成项目时,生成的二进制文件与使用旧版本的 Visual Studio 生成的二进制文件完全兼容。When you build the project, the generated binaries are fully compatible with the ones you built with the older version of Visual Studio. 这是因为 Visual Studio 使用的 C++ 编译器和链接的库与旧版 IDE 所随附的相同。This is because Visual Studio uses the same C++ compiler and links the same libraries that your older IDE shipped with. 这也是为什么如果选择“取消”,升级对话框会警告保留安装较旧的 Visual Studio 版本。That is also why the upgrade dialog warns you to keep the older Visual Studio version installed if you choose Cancel.

有关 Visual Studio 2008 的说明Instructions for Visual Studio 2008

Visual Studio 2008 具有自己的专用 C++ 生成系统,名为 VCBuild。Visual Studio 2008 had its own dedicated build system for C++ called VCBuild. 从 Visual Studio 2010 开始,Visual C++ 项目改为使用 MSBuild。Starting in Visual Studio 2010, Visual C++ projects were changed to use MSBuild. 这意味着必须通过更新步骤在最新版本的 Visual Studio 中生成 Visual Studio 2008项目。This means that you must go through an update step to build your Visual Studio 2008 projects in the latest version of Visual Studio. 更新的项目仍然会生成与使用 Visual Studio 2008 IDE 创建的二进制文件完全兼容的二进制文件。Your updated project still generates binaries that are fully compatible with binaries created using the Visual Studio 2008 IDE.

首先,除了当前版本的 Visual Studio 之外,还必须将 Visual Studio 2010 安装在安装 Visual Studio 2008 的计算机上。First, in addition to the current version of Visual Studio, you must install Visual Studio 2010 on the same computer as Visual Studio 2008. 只有 Visual Studio 2010 会安装面向 Visual Studio 2008 项目时所需的 MSBuild 脚本。Only Visual Studio 2010 installs the MSBuild scripts that are required to target Visual Studio 2008 projects.

接下来,必须将 Visual Studio 2008 解决方案和项目更新到当前版本的 Visual Studio。Next, you must update your Visual Studio 2008 solution and projects to the current version of Visual Studio. 建议在升级之前创建项目和解决方案文件的备份。We recommend you create a backup of your projects and solution files before the upgrade. 若要开始升级过程,请在当前版本的 Visual Studio 中打开解决方案。To start the upgrade process, open the solution in the current version of Visual Studio. 获取升级提示时,请查看显示的信息,然后选择“确定”以开始升级。When you get the upgrade prompt, review the information presented, and then choose OK to start the upgrade. 如果解决方案中有多个项目,则必须更新,向导使用现有 .vcproj 文件并行创建新的 .vcxproj 项目文件。If you have more than one project in the solution, you must update The wizard creates new .vcxproj project files side-by-side with the existing .vcproj files. 只要你也有原始 .sln 文件的副本,升级对你现有的 Visual Studio 2008 项目就没有其他影响。As long as you also have a copy of the original .sln file, the upgrade has no other impact on your existing Visual Studio 2008 projects.

升级完成后,如果日志报告中有关于任何项目的错误或警告,请仔细查看。When the upgrade completes, if the log report has errors or warnings for any of your projects, review them carefully. VCBuild 到 MSBuild 的转换可能会导致问题。The conversion from VCBuild to MSBuild can cause issues. 请确保已了解并执行报告中列出的所有操作项。Make sure you understand and implement any action items listed in the report. 有关升级日志报告和将 VCBuild 转换为 MSBuild 时可能出现的问题的详细信息,请参阅 C++ 本机多目标博客文章。For more information on the upgrade log report and issues that may occur when converting VCBuild to MSBuild, see this C++ Native Multi-Targeting blog post.

项目升级完成且已更正日志文件中的所有问题后,解决方案才真正面向最新的工具集。When the project upgrade is completed, and you have corrected any issues in the log file, your solution actually targets the latest toolset. 最后一步是更改解决方案中每个项目的属性以使用 Visual Studio 2008 工具集。As the final step, change the properties for each project in the solution to use the Visual Studio 2008 toolset. 在当前版本的 Visual Studio 中加载解决方案后,针对解决方案中每个项目,打开项目的“属性页”对话框:右键单击“解决方案资源管理器”中的项目,然后选择“属性”。With the solution loaded in the current version of Visual Studio, for each project in the solution, open the Project Property Pages dialog box: Right-click on the project in Solution Explorer and then select Properties. 在“属性页”对话框中,将“配置”下拉值更改为“所有配置”。In the Property Pages dialog box, change the Configuration drop-down value to All Configurations. 在“配置属性”下,选择“常规”,然后将“平台工具集”更改为 Visual Studio 2008 (v90)In Configuration Properties, select General, and then change Platform Toolset to Visual Studio 2008 (v90).

进行此更改后,在当前版本的 Visual Studio 中构建解决方案时,使用 Visual Studio 2008 编译器和库生成项目二进制文件。After this change, the Visual Studio 2008 compiler and libraries are used to generate project binaries when you build the solution in the current version of Visual Studio.

安装较旧版本的 Visual Studio 工具集Install an older Visual Studio toolset

可能有无法或不打算升级的旧版 Visual C++ 项目,而不是与项目匹配的平台工具集版本。You may have an old Visual C++ project that you can't or don't want to upgrade, but not the platform toolset version that matches your project. 在这种情况下,为了获得工具集,可以安装所需版本的免费 Visual Studio 社区版或速成版。In this case, to get the toolset, you can install the free Visual Studio Community or Express edition of the version you need. 从 Visual Studio 2008 开始,每个版本的 Visual Studio 都可以安装从当前 Visual Studio 面向该版本时所需的编译器、工具和库。Every version of Visual Studio from Visual Studio 2008 on can install the compiler, tools, and libraries you need to target that version from the current Visual Studio. 搜索 Microsoft 下载中心以查找和下载 Visual Studio 的特定版本。Search the Microsoft Download Center to find and download a particular version of Visual Studio. 请确保在安装过程中选择 C++ 安装选项。Make sure you choose the C++ installation options during setup. 安装完成后,运行该版本的 Visual Studio 以安装任何更新。After setup completes, run that version of Visual Studio to install any updates. 同时查找可能需要的任何 Windows 更新更改。Also check for any Windows Update changes that might be required. 此更新检查过程可能需要重复多次以获取每个更新。This update check process may need to be repeated more than once to get every update.

下面是可能会需要的 Visual Studio 下载内容:Here are some of the Visual Studio downloads you might need:

安装这些产品后,“属性页”对话框中的“平台工具集”属性下拉列表自动更新为显示可用工具集。When these products are installed, the Platform Toolset property drop-down in the Property Pages dialog box is automatically updated to show the available toolsets. 现在,可以使用最新版 Visual Studio 为这些较旧版本的工具集生成项目,而无需对这些旧版本进行转换或升级。You can now use the latest version of Visual Studio to build projects for those older versions of the toolset without converting or upgrading them.

请参阅See Also

从 Visual C++ 早期版本升级项目Upgrading Projects from Earlier Versions of Visual C++
Visual Studio 2017 中 C++ 的符合性改进C++ conformance improvements in Visual Studio 2017