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, and pure managed code for .NET development. 所有版本的 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

初始化 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.

如果使用 /clr:pure 编译器开关,则代码将与静态库 msvcurt.lib 链接。If you are using the /clr:pure compiler switch, your code will be linked with the static library msvcurt.lib. /clr一样,你不能与静态链接的库链接。As with /clr, you cannot link with the statically linked library. 从 Visual Studio 2015 开始,/clr:pure/clr:safe 编译器选项已被弃用。The /clr:pure and /clr:safe compiler options are deprecated starting in Visual Studio 2015.

有关将 CRT 与 /clr 配合使用的详细信息,请参阅混合(本机和托管)程序集;对于 /clr: pure,请参阅纯代码和可验证代码 (C + + CLI)For more information on using the CRT with /clr, see Mixed (Native and Managed) Assemblies; for /clr:pure, see Pure and Verifiable Code (C++/CLI).

若要生成你的应用程序的调试版本,则必须定义 _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 多线程动态链接(MSVCP<version>.dll 的导入库)Multithreaded, dynamic link (import library for MSVCP<version>.dll) /MD/MD _MT, _DLL_MT, _DLL
LIBCPMTD.LIBLIBCPMTD.LIB 多线程, 静态链接Multithreaded, static link /MTd/MTd _DEBUG, _MT_DEBUG, _MT
MSVCPRTD.LIBMSVCPRTD.LIB 多线程动态链接(MSVCP<version>D.DLL 的导入库)Multithreaded, dynamic link (import library for MSVCP<version>D.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>   

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

如果有多个 DLL 或 EXE,则无论是否正在使用不同版本的 Visual C++,你都可以具有多个 CRT。If you have more than one DLL or EXE, then you may have more than one CRT, whether or not you are using different versions of Visual C++. 例如,将 CRT 静态链接到多个 Dll 可能存在相同的问题。For example, statically linking the CRT into multiple DLLs can present the same problem. 遇到此静态 CRT 问题的开发人员已被告知使用 /MD 进行编译,以便使用 CRT DLL。Developers encountering this problem with static CRTs have been instructed to compile with /MD to use the CRT DLL. 如果 Dll 跨 DLL 边界传递 CRT 资源,则可能遇到与 CRT 不匹配的问题,需要使用 Visual C++ 重新编译项目。If your DLLs pass CRT resources across the DLL boundary, you may encounter issues with mismatched CRTs and need to recompile your project with Visual C++.

如果程序使用多个版本的 CRT,则跨 DLL 边界传递某些 CRT 对象(如文件句柄、区域设置和环境变量)时需注意。If your program is using more than one version of the CRT, some care is needed when passing certain CRT objects (such as file handles, locales and environment variables) across DLL boundaries. 有关所涉及问题以及如何解决这些问题的详细信息,请参阅跨 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

C 运行时库参考C Run-Time Library Reference