CRT 库功能CRT Library Features

本主题将介绍各种 .lib 文件,包括 C 运行时库及其关联的编译器选项和预处理器指令。This topic discusses the various .lib files that comprise the C run-time libraries as well as their associated compiler options and preprocessor directives.

C 运行时库 (CRT)C Run-Time Libraries (CRT)

C 运行时库 (CRT) 是集成了 ISO C99 标准库的 C++ 标准库。The C Run-time Library (CRT) is the part of the C++ Standard Library that incorporates the ISO C99 standard library. 实现 CRT 的 Visual C++ 库支持用于 .NET 开发的本机代码开发以及本机和托管混合代码。The Visual C++ libraries that implement the CRT support native code development, and both mixed native and managed code. 所有版本的 CRT 都支持多线程开发。All versions of the CRT support multi-threaded development. 大多数的库都支持通过静态链接将库直接链接到代码中,或通过动态链接让代码使用常用 DLL 文件。Most of the libraries support both static linking, to link the library directly into your code, or dynamic linking to let your code use common DLL files.

从 Visual Studio 2015 开始,CRT 已被重构为新的二进制文件。Starting in Visual Studio 2015, the CRT has been refactored into new binaries. 通用 CRT (UCRT) 包含通过标准 C99 CRT 库导出的函数和全局函数。The Universal CRT (UCRT) contains the functions and globals exported by the standard C99 CRT library. UCRT 现为 Windows 组件,并作为 Windows 10 的一部分提供。The UCRT is now a Windows component, and ships as part of Windows 10. 静态库、DLL 导入库和 UCRT 的头文件现在 Windows 10 SDK 中提供。The static library, DLL import library, and header files for the UCRT are now found in the Windows 10 SDK. 安装 Visual C++ 时,Visual Studio 安装程序将安装使用 UCRT 所需 Windows 10 SDK 的子集。When you install Visual C++, Visual Studio setup installs the subset of the Windows 10 SDK required to use the UCRT. 可以在 Visual Studio 2015 及更高版本支持的任何 Windows 版本上使用 UCRT。You can use the UCRT on any version of Windows supported by Visual Studio 2015 and later versions. 可以使用 vcredist 重新分发它,以便支持 Windows 10 以外的 Windows 版本。You can redistribute it using vcredist for supported versions of Windows other than Windows 10. 有关详细信息,请参阅 Redistributing Visual C++ FilesFor more information, see Redistributing Visual C++ Files.

下表列出了实现 UCRT 的库。The following table lists the libraries that implement the UCRT.

Library 关联的 DLLAssociated DLL 特征Characteristics 选项Option 预处理器指令Preprocessor directives
libucrt.liblibucrt.lib None 将 UCRT 静态链接到你的代码。Statically links the UCRT into your code. /MT/MT _MT_MT
libucrtd.liblibucrtd.lib None 用于静态链接的 UCRT 调试版本。Debug version of the UCRT for static linking. 不可再发行。Not redistributable. /MTd/MTd _DEBUG, _MT_DEBUG, _MT
ucrt.libucrt.lib ucrtbase.dllucrtbase.dll UCRT 的 DLL 导入库。DLL import library for the UCRT. /MD/MD _MT, _DLL_MT, _DLL
ucrtd.libucrtd.lib ucrtbased.dllucrtbased.dll UCRT 调试版本的 DLL 导入库。DLL import library for the Debug version of the UCRT. 不可再发行。Not redistributable. /MDd/MDd _DEBUG, _MT, _DLL_DEBUG, _MT, _DLL

vcruntime 库包含 Visual C++ CRT 实现特定的代码,例如异常处理和调试支持、运行时检查和类型信息、实现的详细信息和某些扩展的库函数。The vcruntime library contains Visual C++ CRT implementation-specific code, such as exception handling and debugging support, runtime checks and type information, implementation details and certain extended library functions. 此库特定于所用编译器的版本。This library is specific to the version of the compiler used.

此表列出了实现 vcruntime 库的库。This table lists the libraries that implement the vcruntime library.

Library 关联的 DLLAssociated DLL 特征Characteristics 选项Option 预处理器指令Preprocessor directives
libvcruntime.liblibvcruntime.lib None 静态链接到你的代码。Statically linked into your code. /MT/MT _MT_MT
libvcruntimed.liblibvcruntimed.lib None 用于静态链接的调试版本。Debug version for static linking. 不可再发行。Not redistributable. /MTd/MTd _MT, _DEBUG_MT, _DEBUG
vcruntime.libvcruntime.lib vcruntime<version>.dllvcruntime<version>.dll vcruntime 的 DLL 导入库。DLL import library for the vcruntime. /MD/MD _MT, _DLL_MT, _DLL
vcruntimed.libvcruntimed.lib vcruntime<version>d.dllvcruntime<version>d.dll 调试 vcruntime 的 DLL 导入库。DLL import library for the Debug vcruntime. 不可再发行。Not redistributable. /MDd/MDd _DEBUG, _MT, _DLL_DEBUG, _MT, _DLL

备注

发生 UCRT 重构时,并发运行时函数会移到已添加到 C++ 可再发行包中的 concrt140.dll。When the UCRT refactoring occurred, the Concurrency Runtime functions were moved into concrt140.dll, which was added to the C++ redistributable package. 此 DLL 是 C++ 并行容器和算法(如 concurrency::parallel_for)所必需的。This DLL is required for C++ parallel containers and algorithms such as concurrency::parallel_for. 此外,C++ 标准库需要 Windows XP 版的此 DLL 来支持同步基元,因为 Windows XP 不具有条件变量。In addition, the C++ Standard Library requires this DLL on Windows XP to support synchronization primitives, because Windows XP does not have condition variables.

初始化 CRT 的代码是几个库中的一个,根据 CRT 库是采用静态或动态链接还是本机、托管或混合代码而定。The code that initializes the CRT is in one of several libraries, based on whether the CRT library is statically or dynamically linked, or native, managed, or mixed code. 此代码处理 CRT 启动、内部逐线程数据初始化和终止。This code handles CRT startup, internal per-thread data initialization, and termination. 它特定于所用编译器的版本。It is specific to the version of the compiler used. 此库始终采用动态链接,即使使用动态链接的 UCRT 也是如此。This library is always statically linked, even when using a dynamically linked UCRT.

此表列出了实现 CRT 初始化和终止的库。This table lists the libraries that implement CRT initialization and termination.

Library 特征Characteristics 选项Option 预处理器指令Preprocessor directives
LIBCMT.liblibcmt.lib 将本机 CRT 启动静态链接到你的代码。Statically links the native CRT startup into your code. /MT/MT _MT_MT
libcmtd.liblibcmtd.lib 静态链接本机 CRT 启动的调试版本。Statically links the Debug version of the native CRT startup. 不可再发行。Not redistributable. /MTd/MTd _DEBUG, _MT_DEBUG, _MT
msvcrt.libmsvcrt.lib 与 DLL UCRT 和 vcruntime 一起使用的本机 CRT 启动的静态库。Static library for the native CRT startup for use with DLL UCRT and vcruntime. /MD/MD _MT, _DLL_MT, _DLL
msvcrtd.libmsvcrtd.lib 与 DLL UCRT 和 vcruntime 一起使用的本机 CRT 启动调试版本的静态库。Static library for the Debug version of the native CRT startup for use with DLL UCRT and vcruntime. 不可再发行。Not redistributable. /MDd/MDd _DEBUG, _MT, _DLL_DEBUG, _MT, _DLL
msvcmrt.libmsvcmrt.lib 与 DLL UCRT 和 vcruntime 一起使用的本机和托管混合 CRT 启动的静态库。Static library for the mixed native and managed CRT startup for use with DLL UCRT and vcruntime. /clr/clr
msvcmrtd.libmsvcmrtd.lib 与 DLL UCRT 和 vcruntime 一起使用的本机和托管混合 CRT 启动调试版本的静态库。Static library for the Debug version of the mixed native and managed CRT startup for use with DLL UCRT and vcruntime. 不可再发行。Not redistributable. /clr/clr
msvcurt.libmsvcurt.lib 纯托管 CRT 的已弃用静态库。Deprecated Static library for the pure managed CRT. /clr:pure/clr:pure
msvcurtd.libmsvcurtd.lib 纯托管 CRT 调试版本的已弃用静态库。Deprecated Static library for the Debug version of the pure managed CRT. 不可再发行。Not redistributable. /clr:pure/clr:pure

如果从没有编译器选项(可指定 C ++运行时库)的命令行链接程序,则链接器将使用静态链接的 CRT 库:libcmt.lib、libvcruntime.lib 和 libucrt.lib。If you link your program from the command line without a compiler option that specifies a C run-time library, the linker will use the statically linked CRT libraries: libcmt.lib, libvcruntime.lib, and libucrt.lib.

使用静态链接的 CRT 意味着由 C 运行时库保存的任何状态信息对于 CRT 的该实例而言是本地的。Using the statically linked CRT implies that any state information saved by the C runtime library will be local to that instance of the CRT. 例如,如果当使用静态链接的 CRT 时使用 strtok、_strtok_l、wcstok、_wcstok_l、_mbstok、_mbstok_l,则 strtok 分析器的位置将与链接到另一个静态 CRT 实例的同一进程(但在不同的 DLL 或 EXE 中)的代码中使用的 strtok 状态无关。For example, if you use strtok, _strtok_l, wcstok, _wcstok_l, _mbstok, _mbstok_l when using a statically linked CRT, the position of the strtok parser is unrelated to the strtok state used in code in the same process (but in a different DLL or EXE) that is linked to another instance of the static CRT. 相反,动态链接的 CRT 可共享动态链接到 CRT 的进程中的所有代码的状态。In contrast, the dynamically linked CRT shares state for all code within a process that is dynamically linked to the CRT. 如果使用这些函数新的更安全版本,这一问题则不适用;例如, strtok_s 就不存在此问题。This concern does not apply if you use the new more secure versions of these functions; for example, strtok_s does not have this problem.

由于通过链接到静态 CRT 构建的 DLL 将具有其自己的 CRT 状态,因此不建议以静态方式链接到 DLL 中的 CRT,除非特别需要和需了解这一后果。Because a DLL built by linking to a static CRT will have its own CRT state, it is not recommended to link statically to the CRT in a DLL unless the consequences of this are specifically desired and understood. 例如,如果在加载 DLL(链接到其自己的静态 CRT)的可执行文件中调用 _set_se_translator ,则转换器将不会捕获由 DLL 中的代码生成的任何硬件异常,但会捕获由主可执行文件中的代码生成的硬件异常。For example, if you call _set_se_translator in an executable that loads the DLL linked to its own static CRT, any hardware exceptions generated by the code in the DLL will not be caught by the translator, but hardware exceptions generated by code in the main executable will be caught.

如果使用 /clr 编译器开关,则将通过静态库 msvcmrt.lib 链接代码。If you are using the /clr compiler switch, your code will be linked with a static library, msvcmrt.lib. 静态库将提供托管的代码和本机 CRT 之间的代理。The static library provides a proxy between your managed code and the native CRT. 你无法使用静态链接的 CRT( /MT/MTd 选项)和 /clrYou cannot use the statically linked CRT ( /MT or /MTd options) with /clr. 请改用动态链接的库(/MD/MDd)。Use the dynamically-linked libraries (/MD or /MDd) instead. 纯托管的 CRT 库在 Visual Studio 2015 中已弃用并在 Visual Studio 2017 中不受支持。The pure managed CRT libraries are deprecated in Visual Studio 2015 and unsupported in Visual Studio 2017.

有关将 CRT 与 /clr 配合使用的详细信息,请参阅混合(本机和托管)程序集For more information on using the CRT with /clr, see Mixed (Native and Managed) Assemblies.

若要生成你的应用程序的调试版本,则必须定义 _DEBUG 标志,并且该应用程序必须与其中一个调试版本的库链接。To build a debug version of your application, the _DEBUG flag must be defined and the application must be linked with a debug version of one of these libraries. 有关使用调试版本的库文件的详细信息,请参阅 CRT 调试技术For more information about using the debug versions of the library files, see CRT Debugging Techniques.

此版本的 CRT 不完全符合 C99 标准。This version of the CRT is not fully conformant with the C99 standard. 具体而言,不支持 <tgmath.h> 标头和 CX_LIMITED_RANGE/FP_CONTRACT pragma 宏。In particular, the <tgmath.h> header and the CX_LIMITED_RANGE/FP_CONTRACT pragma macros are not supported. 某些元素(如标准 IO 函数中参数说明符的含义)默认采用旧的解释。Certain elements such as the meaning of parameter specifiers in standard IO functions use legacy interpretations by default. 可以使用 /Zc 编译器一致性选项并指定链接器选项来控制库一致性的某些方面。You can use /Zc compiler conformance options and specify linker options to control some aspects of library conformance,

C++ 标准库C++ Standard Library

C++ 标准库C++ Standard Library 特征Characteristics 选项Option 预处理器指令Preprocessor directives
libcpmt.liblibcpmt.lib 多线程, 静态链接Multithreaded, static link /MT/MT _MT_MT
msvcprt.libmsvcprt.lib 多线程动态链接(MSVCPversion.dll 的导入库)Multithreaded, dynamic link (import library for MSVCPversion.dll) /MD/MD _MT, _DLL_MT, _DLL
libcpmtd.liblibcpmtd.lib 多线程, 静态链接Multithreaded, static link /MTd/MTd _DEBUG, _MT_DEBUG, _MT
msvcprtd.libmsvcprtd.lib 多线程动态链接(MSVCPversionD.DLL 的导入库)Multithreaded, dynamic link (import library for MSVCPversionD.DLL) /MDd/MDd _DEBUG, _MT, _DLL_DEBUG, _MT, _DLL

当构建项目的发行版时,默认情况下,将链接其中一个基本 C 运行时库(libcmt.lib、msvcmrt.lib、msvcrt.lib),具体取决于你选择的编译器选项(多线程、DLL、/clr)。When you build a release version of your project, one of the basic C run-time libraries (libcmt.lib, msvcmrt.lib, msvcrt.lib) is linked by default, depending on the compiler option you choose (multithreaded, DLL, /clr). 如果在代码中包含其中一个 C++ 标准库标头文件,则将在编译时通过 Visual C++ 自动链接 C++ 标准库。If you include one of the C++ Standard Library header files in your code, a C++ Standard Library will be linked in automatically by Visual C++ at compile time. 例如:For example:

#include <ios>

对于二进制兼容性,可通过单个导入库指定多个 DLL 文件。For binary compatibility, more than one DLL file may be specified by a single import library. 版本更新可能会引入点库,点库是引入新的库功能的 单独的 Dll。Version updates may introduce dot libraries, separate DLLs that introduce new library functionality. 例如,Visual Studio 2017 版 15.6 引入了 msvcp140_1.dll 以支持其他标准库功能,而不会破坏由 msvcp140.dll 支持的 ABI。For example, Visual Studio 2017 version 15.6 introduced msvcp140_1.dll to support additional standard library functionality without breaking the ABI supported by msvcp140.dll. 适用于 Visual Studio 2017 版本 15.6 的工具集中包含的 msvcprt.lib 导入库支持两个 DLL,且此版本的 vcredist 安装两个 DLL。The msvcprt.lib import library included in the toolset for Visual Studio 2017 version 15.6 supports both DLLs, and the vcredist for this version installs both DLLs. 传送后,点库具有固定 ABI,且不会在以后的点库中包含依赖项。Once shipped, a dot library has a fixed ABI, and will never have a dependency on a later dot library.

如果应用程序使用多个 CRT 版本,将存在什么问题?What problems exist if an application uses more than one CRT version?

每个可执行映像(EXE 或 DLL)可以具有其自己静态链接的 CRT,或可以动态链接到 CRT。Every executable image (EXE or DLL) can have its own statically linked CRT, or can dynamically link to a CRT. 静态包括在某个映像中或某个映像动态加载的 CRT 版本取决于构建 该 CRT 时采用的工具和库。The version of the CRT statically included in or dynamically loaded by a particular image depends on the version of the tools and libraries it was built with. 单个进程可能会加载多个 EXE 和 DLL 映像,每个都有其自己的 CRT。A single process may load multiple EXE and DLL images, each with its own CRT. 每个 CRT 可能使用不同的分配器,可能具有不同的内部结构布局,可能使用不同的存储排列方式。Each of those CRTs may use a different allocator, may have different internal structure layouts, and may use different storage arrangements. 这意味着,分配的内存、CRT 资源或跨 DLL 边界传递的类可能会导致内存管理、内部静态使用情况或布局解释方面的问题。This means that allocated memory, CRT resources, or classes passed across a DLL boundary can cause problems in memory management, internal static usage, or layout interpretation. 例如,如果在一个 DLL 中分配类,但将其传递给另一个 DLL 或由另一个 DLL 删除,那么使用了哪个 CRT 释放器?For example, if a class is allocated in one DLL but passed to and deleted by another, which CRT deallocator is used? 导致的错误程度可以从微小到立即致命,因此强烈建议不要直接传输此类资源。The errors caused can range from the subtle to the immediately fatal, and therefore direct transfer of such resources is strongly discouraged.

你可以使用应用程序二进制接口 (ABI) 技术避免这些问题,因为此技术被设计成稳定且版本可控。You can avoid many of these issues by using Application Binary Interface (ABI) technologies instead, as they are designed to be stable and versionable. 设计 DLL 导出接口以按值传递信息,或致力于调用方传入而非本地分配并返回给调用方的内存。Design your DLL export interfaces to pass information by value, or to work on memory that is passed in by the caller rather than allocated locally and returned to the caller. 使用封送技术复制可执行映像之间的结构化数据。Use marshalling techniques to copy structured data between executable images. 本地封装资源并仅允许通过向客户端公开的句柄或函数操作。Encapsulate resources locally and only allow manipulation through handles or functions you expose to clients.

如果进程中的所有映像全都使用相同的 CRT 动态加载版本,则也有可能避免这些问题。It's also possible to avoid some of these issues if all of the images in your process use the same dynamically loaded version of the CRT. 若要确保所有组件都使用相同的 CRT 的 DLL 版本,请使用“/MD”选项,并使用相同的编译器工具集和属性设置进行构建。To ensure that all components use the same DLL version of the CRT, build them by using the /MD option, and use the same compiler toolset and property settings.

如果程序跨 DLL 边界传递某些 CRT 资源(如文件句柄、区域设置和环境变量),即便使用的是相同版本的 CRT,那也需要注意。Some care is needed if your program passes certain CRT resources (such as file handles, locales and environment variables) across DLL boundaries, even when using the same version of the CRT. 有关所涉及问题以及如何解决这些问题的详细信息,请参阅跨 DLL 边界传递 CRT 对象时可能的错误For more information on the issues involved and how to resolve them, see Potential Errors Passing CRT Objects Across DLL Boundaries.

请参阅See also