将代码升级到通用 CRTUpgrade your code to the Universal CRT

在 Visual Studio 2015 中,重构了 Microsoft C 运行时库 (CRT)。In Visual Studio 2015, the Microsoft C Runtime Library (CRT) was refactored. 将标准 C 库、POSIX 扩展和 Microsoft 特定的函数、宏和全局变量移动到了新库,即通用 C 运行时库(通用 CRT 或 UCRT)。The Standard C Library, POSIX extensions and Microsoft-specific functions, macros, and global variables were moved into a new library, the Universal C Runtime Library (Universal CRT or UCRT). 将 CRT 特定于编译器的组件移动到了新的 vcruntime 库中。The compiler-specific components of the CRT were moved into a new vcruntime library.

UCRT 现为 Windows 组件,并作为 Windows 10 的一部分提供。The UCRT is now a Windows component, and ships as part of Windows 10. UCRT 支持基于 C 调用约定的稳定 ABI,且谨遵 ISO C99 标准(仅有少数例外)。The UCRT supports a stable ABI based on C calling conventions, and it conforms closely to the ISO C99 standard, with only a few exceptions. 它将不再绑定到特定版本的编译器。It is no longer tied to a specific version of the compiler. 可以在 Visual Studio 2015 或 Visual Studio 2017 支持的任何 Windows 版本上使用 UCRT。You can use the UCRT on any version of Windows supported by Visual Studio 2015 or Visual Studio 2017. 其好处是使用者不再需要在每次升级 Visual Studio 时都以新版本的 CRT 为目标来更新自己的版本。The benefit is that you no longer need to update your builds to target a new version of the CRT with every upgrade of Visual Studio.

使用此重构时,许多 CRT 头文件、库文件和可再发行组件的名称或位置以及代码所需的部署方法都有所改变。With this refactoring, the names or locations of many CRT header files, library files, and redistributables, and the deployment methods required for your code have changed. 此外,还在 UCRT 中添加或更改了许多功能和宏以提高对标准的一致性。In addition, many functions and macros in the UCRT were added or changed to improve standards conformance. 若要利用这些更改,必须更新现有代码和项目生成系统。To take advantage of these changes, your existing code and project build systems must be updated.

在哪里可以找到通用 CRT 文件Where to find the Universal CRT files

作为 Windows 组件,UCRT 库文件和标头现在是 Windows 软件开发工具包 (SDK) 的一部分。As a Windows component, the UCRT library files and headers are now part of the Windows software development kit (SDK). 安装 Visual Studio 时,会同时安装使用 UCRT 时所需的 Windows SDK 的部件。When you install Visual Studio, the parts of the Windows SDK required to use the UCRT are also installed. Visual Studio 安装程序将 UCRT 头、库和 DLL 文件的位置添加到 Visual Studio 项目生成系统所使用的默认路径。The Visual Studio installer adds the locations of the UCRT headers, libraries and DLL files to the default paths used by the Visual Studio project build system. 当更新 Visual C++ 项目时,如果它们使用默认项目设置,IDE 将自动为头文件查找新位置,并且链接器会自动使用新的默认 UCRT 和 vcruntime 库。When you update your Visual C++ projects, if they use the default project settings, the IDE automatically finds the new locations for header files, and the linker automatically uses the new default UCRT and vcruntime libraries. 同样,如果使用开发人员命令提示执行命令行生成,则包含头和库路径的环境变量也会更新并自动工作。Similarly, if you use a Developer command prompt to do command-line builds, the environment variables that contain paths for headers and libraries are updated and work automatically as well.

标准 C 库头文件现可在 Windows SDK 中找到,其位于特定于 SDK 版本的目录的包含文件夹中。The Standard C Library header files are now found in the Windows SDK in an include folder in an SDK version-specific directory. 头文件的常规位置在 Windows Kits\10\Include\sdk-version\ucrt 下的 Program Files 或 Program Files (x86) 目录中,其中 sdk-version 对应于 Windows 版本或更新,例如,用于 Windows 10 周年更新的 10.0.14393.0。A typical location for the header files is in the Program Files or Program Files (x86) directory under Windows Kits\10\Include\sdk-version\ucrt, where sdk-version corresponds to a Windows version or update, for example, 10.0.14393.0 for the Anniversary Update of Windows 10.

UCRT 静态库和动态链接存根库位于 Windows Kits\10\Lib\sdk-version\ucrt\architecture 下的 Program Files 或 Program Files (x86) 目录下,其中 architecture 是 ARM、x86 或 X64。The UCRT static libraries and dynamic link stub libraries are found in the Program Files or Program Files (x86) directory under Windows Kits\10\Lib\sdk-version\ucrt\architecture, where architecture is ARM, x86, or X64. 零售和调试静态库分别是 libucrt.lib 和 libucrtd.lib,用于 UCRT DLL 的库是 ucrt.lib 和 ucrtd.lib。The retail and debug static libraries are libucrt.lib and libucrtd.lib, and the libraries for the UCRT DLLs are ucrt.lib and ucrtd.lib.

零售和调试 UCRT DLL 位于不同的位置。The retail and debug UCRT DLLs are found in separate locations. 零售 DLL 可再发行,可以在 Program Files 或 Program Files (x86) 目录的 Windows Kits\10\Redist\ucrt\DLLs\architecture. 中找到The retail DLLs are redistributable, and can be found in the Program Files or Program Files (x86) directory under Windows Kits\10\Redist\ucrt\DLLs\architecture. 调试 UCRT 库不可再发行,可以在 Program Files 或 Program Files (x86) 目录的 Windows Kits\10\bin\architecture\ucrt 文件夹中找到。Debug UCRT libraries are not redistributable, and can be found in the Program Files or Program Files (x86) directory under Windows Kits\10\bin\architecture\ucrt folder.

C 和 C++ 编译器特定的运行时支持库 vcruntime ,包含支持程序启动所需的代码以及异常处理和内部函数等功能。The C and C++ compiler-specific runtime support library, vcruntime, contains the code required to support program startup and features such as exception handling and intrinsics. 库及其头文件仍位于 Program Files 或 Program files (x86) 目录中特定于版本的 Microsoft Visual Studio 文件夹中。The library and its header files are still found in the version-specific Microsoft Visual Studio folder in your Program Files or Program files (x86) directory. 在 Visual Studio 2017 中,标头位于 Microsoft Visual Studio\2017\edition\VC\Tools\MSVC\lib-version\include 中,链接库位于 Microsoft Visual Studio\2017\edition\VC\Tools\MSVC\lib-version\lib\architecture 中,其中 edition 是安装的 Visual Studio 版本,lib-version 是库的版本,而 architecture 是处理器体系结构。In Visual Studio 2017, the headers are found under Microsoft Visual Studio\2017\edition\VC\Tools\MSVC\lib-version\include and the link libraries are found under Microsoft Visual Studio\2017\edition\VC\Tools\MSVC\lib-version\lib\architecture, where edition is the edition of Visual Studio installed, lib-version is the version of the libraries, and architecture is the processor architecture. OneCore 和 Store 的链接库也位于库文件夹中。Link libraries for OneCore and Store are also found in the libraries folder. 静态库的零售和调试版本分别是 libvcruntime.lib 和 libvcruntimed.lib。The retail and debug versions of the static library are libvcruntime.lib and libvcruntimed.lib. 动态链接零售和调试存根库分别是 vcruntime.lib 和 vcruntimed.lib。The dynamic link retail and debug stub libraries are vcruntime.lib and vcruntimed.lib, respectively.

更新 Visual C++ 项目时,如果将项目的链接器属性“忽略所有默认库”设置为“是”,或如果使用的是命令行上的 /NODEFAULTLIB 链接器选项,则必须更新库的列表以包括新的重构库。When you update your Visual C++ projects, if you have set the project's Linker property Ignore All Default Libraries to Yes or if you use the /NODEFAULTLIB linker option on the command line, then you must update your list of libraries to include the new, refactored libraries. 将旧的 CRT 库(例如 libcmt.lib、libcmtd.lib、msvcrt.lib 或 msvcrtd.lib)替换为等效的重构库。Replace the old CRT library, for example, libcmt.lib, libcmtd.lib, msvcrt.lib, or msvcrtd.lib, with the equivalent refactored libraries. 有关要使用的特定库的信息,请参阅 CRT 库的功能For information on the specific libraries to use, see CRT Library Features.

通用 CRT 的部署和重新分发Deployment and redistribution of the Universal CRT

因为 UCRT 现在是Microsoft Windows 操作系统组件,它作为操作系统的一部分包含在 Windows 10 中,可通过较旧操作系统(Windows Vista 到 Windows 8.1)的 Windows 更新获得。Because the UCRT is now a Microsoft Windows operating system component, it is included as part of the operating system in Windows 10 and is available through Windows Update for older operating systems, Windows Vista through Windows 8.1. 有一个可重新分发的版本适用于 Windows XP。A redistributable version is available for Windows XP. 作为操作系统组件,UCRT 更新和服务由 Windows 更新进行管理(独立于 Visual Studio 和 Visual C++ 编译器版本)。As an operating system component, UCRT updates and servicing are managed by Windows Update independently of Visual Studio and Visual C++ compiler versions. 由于 UCRT 是 Windows 组件,出于安全性、易于更新以及更小的映像大小的考虑,强烈建议为应用集中部署 UCRT。Because the UCRT is a Windows component, for security and ease of updates, and a smaller image size, we strongly recommend central deployment of the UCRT for your app.

可以在 Visual Studio 2015 或 Visual Studio 2017 支持的任何 Windows 版本上使用 UCRT。You can use the UCRT on any version of Windows supported by Visual Studio 2015 or Visual Studio 2017. 可以使用 vcredist 包重新分发它,以便支持 Windows 10 以外的 Windows 版本。You can redistribute it using a vcredist package for supported versions of Windows other than Windows 10. Vcredist 包包含 UCRT 组件,并自动将这些组件安装在默认情况下不安装它们的 Windows 操作系统上。The vcredist packages include the UCRT components and automatically install them on Windows operating systems that do not have them installed by default. 有关详细信息,请参阅重新分发 Visual C++ 文件For more information, see Redistributing Visual C++ Files.

支持 UCRT 的本地应用部署(尽管由于性能和安全原因不推荐)。App-local deployment of the UCRT is supported, though not recommended for both performance and security reasons. 用于本地应用部署的 DLL 作为 Windows SDK 的一部分包含在 redist 子目录下。The DLLs for app-local deployment are included as part of the Windows SDK, under the redist subdirectory. 所需的 DLL 包括 ucrtbase.dll 和名为 api-ms-win-subset.dll. 的一组 APISet forwarder DLL。The DLLs required include ucrtbase.dll and a set of APISet forwarder DLLs named api-ms-win-subset.dll. 每个操作系统所需的 DLL 集各不相同,因此建议在使用应用本地部署时包括所有 DLL。The set of DLLs required on each operating system varies, so we recommended that you include all of the DLLs when you use app-local deployment. 有关应用本地部署的其他详细信息和注意事项,请参阅 Visual C++ 中的部署For additional details and caveats about app-local deployment, see Deployment in Visual C++.

对通用 CRT 函数和宏的更改Changes to the Universal CRT functions and macros

已在 UCRT 中添加或更新许多功能,以提高 ISO C99 一致性和解决代码质量和安全问题。Many functions were added or updated in the UCRT to improve ISO C99 conformance, and to address code quality and security issues. 在某些情况下,这需要中断对库的更改。In some cases, this required breaking changes to the library. 如果在使用较旧版本的 CRT 时代码编译顺畅,但在使用 UCRT 时编译中断,则必须更改代码以利用这些更新和功能。If your code compiled cleanly when using an older version of the CRT but breaks when compiled using the UCRT, you must change your code to take advantage of these updates and features. 有关通用 CRT 中出现的 CRT 更改和更新中断情况的详细列表,请参阅 Visual C++ 更改历史记录的 C 运行时库 (CRT) 部分。For a detailed listing of the breaking changes and updates to the CRT found in the Universal CRT, see the C Runtime Library (CRT) section of the Visual C++ change history. 它包括受影响的标头和函数的列表,可以使用它们来标识代码中需要的更改。It includes a list of affected headers and functions that you can use to identify the changes needed in your code.

另请参阅See Also

Visual C++ 移植和升级指南Visual C++ Porting and Upgrading Guide
潜在的升级问题概述 (Visual C++)Overview of potential upgrade issues (Visual C++)
从 Visual C++ 早期版本升级项目Upgrading Projects from Earlier Versions of Visual C++
Visual C++ 更改历史记录(2003 - 2015)Visual C++ change history 2003 - 2015
Visual Studio 2017 中 C++ 的符合性改进C++ conformance improvements in Visual Studio 2017