Visual C++ 移植和升级指南Visual C++ Porting and Upgrading Guide

本主题提供有关升级 Visual c++ 代码的指南。This topic provides a guide for upgrading Visual C++ code. 它包括获取要编译和在新版本的工具上运行的代码,以及利用新语言和 Visual Studio 功能。This includes getting the code to compile and run correctly on a newer release of the tools, as well as taking advantage of new language and Visual Studio features. 本主题还包括有关将原有应用迁移到更现代的平台的信息。This topic also includes information about migrating legacy apps to more modern platforms.

升级 Visual C++ 代码的原因Reasons to Upgrade Visual C++ Code

应考虑出于以下原因升级你的代码:You should consider upgrading your code for the following reasons:

  • 由于改进了编译器优化,可实现更快的代码。Faster code, due to improved compiler optimizations.

  • 由于编译器本身性能的改进,可实现更快的生成。Faster builds, due to performance improvements in the compiler itself.

  • 改进了标准符合性。Improved standards conformance. 现在 Visual C++ 可依照最新的 C++ 标准实现许多功能。Visual C++ now implements many features from the latest C++ standards.

  • 更好的安全性。Better security. 安全功能,如保护检查。Security features such as guard checking.

移植代码Porting your Code

升级时,首先考虑应用程序代码和项目。When upgrading, first consider your application's code and projects. 你的应用程序是使用 Visual Studio 生成的吗?Is your application built with Visual Studio? 如果是,确定所涉及的项目。If so, identify the projects involved. 是否拥有自定义生成脚本?Do you have custom build scripts? 如果拥有自定义生成脚本,而不是使用 Visual Studio 的生成系统,升级中将需要完成更多的工作,因为无法通过使用 Visual Studio 升级项目文件和生成设置来节省时间。If you have custom build scripts instead of using Visual Studio's build system, you will have more work to do in upgrading, because you can't save time by having Visual Studio update your project files and build settings.

Visual Studio 中的生成系统和项目文件格式从 Visual Studio 2008 及之前版本中的 vcbuild 更改为 Visual Studio 2010 及之后版本中的 MSBuild。The build system and project file format in Visual Studio changed from vcbuild in versions up to Visual Studio 2008 to MSBuild in versions of Visual Studio from 2010 onwards. 如果是从 2010 之前的版本升级,并且具有高度自定义的生成系统,可能不得不完成更多的工作以进行升级。If your upgrade is from a version prior to 2010, and you have a highly customized build system, you might have to do more work to upgrade. 如果是从 Visual Studio 2010 及之后的版本升级,则你的项目已经在使用 MSBuild 了,所以为应用程序升级项目和生成应该会比较容易。If you are upgrading from Visual Studio 2010 or later, your projects are already using MSBuild, so upgrading the project and build for your application should be easier.

如果不使用 Visual Studio 的生成系统,则应考虑升级以使用 MSBuild。If you are not using Visual Studio's build system, you should consider upgrading to use MSBuild. 如果升级以使用 MSBuild,可能在以后的升级中会更轻松,使用诸如 Visual Studio Online 等服务将会更容易。If you upgrade to use MSBuild, you might have an easier time in future upgrades, and it will be easier to use services such as Visual Studio Online. MSBuild 支持 Visual Studio 支持的所有目标平台。MSBuild supports all the target platforms that Visual Studio supports.

移植 Visual Studio 项目Porting Visual Studio Projects

要开始升级项目或解决方案,只需在新版本的 Visual Studio 中打开解决方案,然后按提示开始对其进行升级。To start upgrading a project or solution, just open the solution in the new version of Visual Studio, and follow the prompts to start upgrading it. 升级项目时,你将获取一个升级报告,该报告也将在你的项目文件夹中保存为 UpgradeLog.htm。When you upgrade a project, you get an upgrade report, which is also saved in your project folder as UpgradeLog.htm. 该升级报告显示有关升级过程中所遇到的问题的总结和有关所做更改或无法自动解决的问题的一些信息。The upgrade report shows a summary of what problems were encountered during the upgrade process and some information about changes that were made, or problems that could not be addressed automatically.

  1. 项目属性Project properties

  2. 包含文件Include files

  3. 由于编译器符合性的改进或标准中的更改而无法完全编译的代码Code that no longer compiles cleanly due to compiler conformance imrovements or changes in the standard

  4. 依赖于不再可用的 Visual Studio 或 Windows 功能或未包含在 Visual Studio 默认安装中或已从产品中移除的标头文件的代码Code that relies on Visual Studio or Windows features that are no longer available or header files that either aren't included in a default installation of Visual Studio, or were removed from the product

  5. 由于 API 中的更改(如 API 重命名、函数签名更改或函数弃用)而不再编译的代码Code that no longer compiles due to changes in APIs such as renamed APIs, changed function signatures, or deprecated functions

  6. 由于诊断中的更改(如警告变为错误)而不再编译的代码Code that no longer compiles due to changes in diagnostics, such as warning becoming an error

  7. 由于库更改导致的链接器错误(尤其是使用 /NODEFAULTLIB 时)。Linker errors due to libraries that were changed, especially when /NODEFAULTLIB is used.

  8. 行为更改所导致的运行时错误或意外结果Runtime errors or unexpected results due to behavior changes

  9. 由于这些工具中引入的错误而导致的错误。Errors due to errors that were introduced in the tools. 如果遇到问题,请通过正常支持渠道或通过使用 Visual Studio 反馈中心将其报告给 Visual C++ 团队。If you encounter an issue, report it to the Visual C++ team through your normal support channels or by using the Visual Studio Feedback Center.

    除了由于编译器错误所导致的无法避免的更改,某些更改在升级过程中是可选的,如:In addition to changes that you can't avoid due to compiler errors, some changes are optional in an upgrade process, such as:

  10. 新的警告可能意味着你想要清理你的代码。New warnings might mean you want to clean up your code. 具体取决于特定诊断,这可以提高你的代码的可移植性、标准符合性和安全性。Depending on the specific diagnostics, this can improve the portability, standards conformance, and security of your code.

  11. 利用添加对未授权代码执行的检查的编译器新功能,如 /guard:cf(启用流控制保护)编译器选项。You might want to take advantage of newer compiler features such as the /guard:cf (Enable Flow Control Guard) compiler option, which adds checks for unauthorized code execution.

  12. 你可能想要更新一些代码以使用可简化代码的新语言功能、提高程序的性能,或更新代码以使用现代库并遵循现代标准和最佳做法。You might want to update some code to use new language features that simplify the code, improve the performance of your programs, or update the code to use modern libraries and conform to modern standards and best practices.

    升级并测试项目后,可能需要考虑进一步改善代码或规划代码的未来方向,或甚至重新考虑项目的体系结构。Once you've upgraded and tested your project, you might also want to consider improving your code further or plan the future direction of your code, or even reconsider the architecture of your project. 它是否会接收进行中的开发工作?Will it receive ongoing development work? 代码可在其他平台上运行是否重要?Will it be important for your code to run on other platforms? 如果是,是哪些平台?If so, what platforms? C++ 是充分考虑了可移植性和跨平台开发而设计的标准化语言,但许多 Windows 应用程序的代码在很大程度上依赖于 Windows 平台。C++ is a standardized langauge designed with portability and cross-platform development in mind, and yet the code for many Windows applications is strongly tied to the Windows platform. 是否想重构代码以分隔出那些更依赖于 Windows 平台的那部分代码?Do you want to refactor your code, to separate out those parts that are more tied to the Windows platform?

    你的用户界面呢?What about your user interface? 如果你正在使用 MFC,可能会想要更新 UI。If you are using MFC, you might want to update the UI. 你是否正在使用在 2008 年作为功能包引入的任何更新的 MFC 功能?Are you using any of the newer MFC features that were introduced in 2008 as a Feature Pack? 如果只是想要让应用获得更新的外观和感觉,而不重写整个应用,则可以考虑使用 MFC 中的功能区 API 或使用 MFC 的一些新功能。If you just want to give your app a newer look and feel, without rewriting the entire app, you might consider using the ribbon APIs in MFC, or using some of new features of MFC.

    如果想要为程序提供 XAML 用户界面,但不想创建 UWP 应用,可以将 C# 与 WPF 搭配使用以创建 UI 层,并将标准 C ++ 逻辑重构到 DLL。If you want to give your program a XAML user-interface but don't want to create a UWP app, you can use C# with WPF to create the UI layer and refactor your standard C++ logic into DLLs. 在 C++/CLI 中创建互操作性层,以连接 C# 和本机代码。Create an interoperability layer in C++/CLI to connect C# with your native code. 创建 UWP 应用的另一种方法是使用 C++/CXC++/WinRTAnother option is to create a UWP app using C++/CX or C++/WinRT. 在 Windows 10 中,可以使用 Desktop App Converter 将现有桌面应用程序打包为 UWP 应用,而无需修改任何代码。In Windows 10, you can use the Desktop App Converter to package your existing desktop application as a UWP app without having to modify any code.
    或者,也许你现在有新的需求,或可以预见到需要面向除 Windows 桌面外的平台,如 Windows Phone 或 Android 设备。Alternatively, perhaps you now have new requirements, or you can foresee the need for targeting platforms other than Windows desktop, such as Windows Phone, or Android devices. 你可以将你的用户界面代码移植到跨平台的 UI 库。You could port your user interface code to a cross-platform UI library. 通过这些 UI 框架,可以面向多个设备并仍然使用 Visual Studio 和 Visual Studio 调试器作为开发环境。With these UI frameworks, you can target multiple devices and still use Visual Studio and the Visual Studio debugger as your development environment.

标题Title 说明Description
从 Visual C++ 早期版本升级项目Upgrading Projects from Earlier Versions of Visual C++ 讨论如何使用在早期版本的 Visual C++ 中创建的项目。Discusses how to use projects created in earlier versions of Visual C++.
Visual Studio 2017 RC 中 Visual C++ 的新增功能What's New for Visual C++ in Visual Studio 2017 RC 从 Visual Studio 2015 到 Visual Studio 2017 的 IDE 和工具更改Changes in the IDE and tools from Visual Studio 2015 to Visual Studio 2017
Visual Studio 2017 中 C++ 的符合性改进C++ conformance improvements in Visual Studio 2017 从 Visual Studio 2015 到 Visual Studio 2017 的标准符合性改进Standards conformance improvements from Visual Studio 2015 to Visual Studio 2017
Visual C++ 更改历史记录(2003 - 2015)Visual C++ change history 2003 - 2015 导致代码可能需要更改的 Visual C++ 库和生成工具中的所有更改的列表(从 Visual Studio 2003 到 Visual Studio 2015)。A list of all the changes in the Visual C++ libraries and build tools from Visual Studio 2003 through 2015 that might require changes in your code.
Visual C++ 新增功能(2003 - 2015)Visual C++ What's New 2003 through 2015 从 Visual Studio 2003 到 Visual Studio 2015 的所有 Visual C++“新增功能”信息。All the "what's new" information for Visual C++ from Visual Studio 2003 through Visual Studio 2015.
移植第三方库Porting 3rd-party libraries 如何使用 vcpkg 命令行工具将较旧的开源库移植到使用较新的 Visual C++ 工具集编译的版本。How to use the vcpkg command line tool to port older open-source libraries to versions compiled with more recent Visual C++ toolsets.
移植和升级:示例和案例研究Porting and Upgrading: Examples and Case Studies 本部分中,我们移植和升级了多个示例和应用程序并讨论了体验和结果。For this section, we ported and upgrades several samples and applications and discussed the experiences and results. 你可能会发现阅读这些内容会使你了解移植和升级过程中所涉及的内容。You might find that reading these gives you a sense of what is involved in the porting and upgrading process. 在整个过程中,我们讨论了升级所用的提示和技巧,并演示如何修复特定错误。Throughout the process, we discuss tips and tricks for upgrading and show how specific errors were fixed.
移植到通用 Windows 平台Porting to the Universal Windows Platform 包含有关移植代码到 Windows 10 的信息Contains information about porting code to Windows 10
Visual C++ 简介(针对 UNIX 用户)Introduction to Visual C++ for UNIX Users 为不熟悉 Visual C++ 并想要有效率的使用它的 UNIX 用户提供信息。Provides information for UNIX users who are new to Visual C++ and want to become productive with it.
从 UNIX 移植到 Win32Porting from UNIX to Win32 讨论用于将 UNIX 应用程序迁移到 Windows 的选项。Discusses options for migrating UNIX applications to Windows.
C++/CLI 迁移入门C++/CLI Migration Primer 详细演示如何升级 C++ 语法的托管扩展来使用新的语法。Shows in detail how to upgrade your Managed Extensions for C++ syntax to use the new syntax. 有关详细信息,请参阅运行时平台的组件扩展For more information, see Component Extensions for Runtime Platforms.

另请参阅See Also

Visual C++Visual C++