VC中的C和C++运行时库

C Run-Time Libraries

**Visual Studio 2010**

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 Run-Time Libraries (CRT)

The following libraries contain the C run-time library functions.

C run-time library (without iostream or standard C++ library)

 Associated DLL

 Characteristics

 Option

 Preprocessor directives

 libcmt.lib

 None, static link

 Multithreaded, static link

 /MT

 _MT

 libcmtd.lib

 None, static link

 Multithreaded, static link (debug)

 /MTd

 _DEBUG, _MT

 msvcrt.lib

 msvcr100.dll

 Multithreaded, dynamic link (import library for MSVCR100.DLL). Be aware that if you use the Standard C++ Library, your program will need MSVCP100.DLL to run.

 /MD

 _MT, _DLL

 msvcrtd.lib

 msvcr100d.dll

 Multithreaded, dynamic link (import library for MSVCR100D.DLL) (debug)

 /MDd

 _DEBUG, _MT, _DLL

 msvcmt.lib

 None, static link

 C Runtime static library. Used for mixed managed/native code.

 /clr, /clr:oldSyntax

 

 msvcurt.lib

 None, static link

 C Runtime static library compiled as 100% pure MSIL code. All code complies with the ECMA URT spec for MSIL.

 /clr:pure

 

> **Note**

> The single-threaded CRT (libc.lib, libcd.lib) (formerly the /ML or /MLd options) is no longer available. Instead, use the multithreaded CRT. See *Multithreaded Libraries Performance*.

If you link your program from the command line without a compiler option that specifies a C run-time library, the linker will use LIBCMT.LIB. This is different from previous versions of Visual C++ which used LIBC.LIB, the single-threaded library, instead.

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. 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. In contrast, the dynamically linked CRT shares state for all code within a process that is dynamically linked to the CRT. 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.

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. 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.

If you are using the /clr compiler switch, your code will be linked with a static library, msvcmt.lib. The static library provides a proxy between your managed code and the native CRT. You cannot use the statically linked CRT (/MT or /MTd options) with /clr. Use the dynamically-linked libraries (/MD or /MDd) instead.

If you are using the /clr:pure compiler switch, your code will be linked with the static library msvcurt.lib. As with /clr, you cannot link with the statically linked library.

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)*.

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. For more information about using the debug versions of the library files, see *CRT Debugging Techniques*.

This version of Visual C++ is not conformant with the C99 standard.

Standard C++ Library

Standard C++ Library

Characteristics

Option

Preprocessor directives

LIBCPMT.LIB

Multithreaded, static link

/MT

_MT

LIBCPMTD.LIB

Multithreaded, static link

/MTd

_DEBUG, _MT

MSVCPRT.LIB

Multithreaded, dynamic link (import library for MSVCP100.dll)

/MD

_MT, _DLL

MSVCPRTD.LIB

Multithreaded, dynamic link (import library for MSVCP100D.DLL)

/MDd

_DEBUG, _MT, _DLL

> **Note:** LIBCP.LIB and LIBCPD.LIB (via the old /ML and /MLd options) have been removed. Use LIBCPMT.LIB and LIBCPMTD.LIB instead via the /MT and /MTd options.

When you build a release version of your project, one of the basic C run-time libraries (LIBCMT.LIB, MSVCMT.LIB, MSVCRT.LIB) is linked by default, depending on the compiler option you choose (multithreaded, DLL, /clr). If you include one of the Header Files in your code, a Standard C++ Library will be linked in automatically by Visual C++ at compile time. For example:

#include <ios>

What is the difference between msvcrt.dll and msvcr100.dll?

The msvcrt.dll is now a "known DLL", meaning that it is a system component owned and built by Windows. It is intended for future use only by system-level components.

What problems exist if an application uses both msvcrt.dll and msvcr100.dll?

If you have a .lib or .obj file that needs to link to msvcrt.lib, then you should not have to recompile it to work with the new msvcrt.lib in Visual C++ 2010. The .lib or .obj file may rely on the sizes, field offsets, or member function names of various CRT classes or variables, and those should all still exist in a compatible way. When you relink against msvcrt.lib, your final EXE and DLL image will now have a dependency on msvcr100.dll instead of msvcrt.dll.

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++. For example, statically linking the CRT into multiple DLLs can present the same problem. Developers encountering this problem with static CRTs have been instructed to compile with /MD to use the CRT DLL. Now that the CRT DLL has been renamed to msvcr100.dll, applications may have some components linked to msvcrt.dll and others to msvcr100.dll. If your DLLs pass CRT resources across the msvcrt.dll and msvcr100.dll boundary, you will encounter issues with mismatched CRTs and need to recompile your project with Visual C++ 2010.

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. For more information on the issues involved and how to resolve them, see Potential Errors Passing CRT Objects Across DLL Boundaries.

Windows 通用 C 运行库

(Universal C Runtime, 简称 UCRT)‌ 是微软在 Windows 10 及后续版本中引入的核心系统组件,旨在统一和标准化 Windows 平台上的 C 语言运行时环境。

1. 核心定义与作用

UCRT 实现了 C99 (ISO/IEC 9899:1999)标准库的大部分功能,同时也包含了 POSIX.1 标准库的大型子集。它的主要目的是将 C 运行时库从特定的 Visual Studio 版本中解耦出来,使其成为 Windows 操作系统的一部分。

统一性‌:在 UCRT 出现之前,每个版本的 Visual Studio (如 VS2005, VS2010, VS2013) 都有自己独立的 CRT DLL(如 msvcr100.dll, msvcr120.dll),导致“DLL Hell”问题。UCRT 将所有标准 C 函数集中到一个统一的库中 (ucrtbase.dll)。

操作系统集成‌:从 Windows 10 开始,UCRT 作为 Windows 更新的一部分直接内置于系统中。对于 Windows 7、8.1 等旧系统,可以通过 Windows Update 或独立安装包获取。

2. 技术架构变化 (VS2015 及以后)

从 Visual Studio 2015 (VC++ 14.0) 开始,微软重构了 C 运行时库的组成,将其拆分为三个部分:

UCRT (Universal C Runtime)‌:包含标准的 C 库函数(如 printf, malloc, fopen 等)。这部分与 Windows 系统绑定,不再随 VS 版本频繁变更。

VCRuntime‌:包含特定于 Microsoft Visual C++ 的实现细节、内部辅助函数以及 C++ 特有的运行时支持(如异常处理、RTTI)。这部分仍随 Visual Studio 版本更新(如 vcruntime140.dll)。

C++ Standard Library (STL)‌:包含 C++ 标准模板库的实现。

这种分离意味着,只要你的应用程序使用的是 VS2015 或更高版本编译,并且依赖的是最新的 VC++ Redistributable,它就可以利用系统中已存在的 UCRT,从而减少部署体积并提高兼容性。

3. 兼容性与支持

二进制兼容性‌:从 Visual Studio 2015 到 2022 (以及未来的 2026+ 版本),使用这些工具构建的应用程序在二进制层面上是兼容的。这意味着你只需要安装最新版本的 ‌Microsoft Visual C++ Redistributable‌,即可运行由 VS2015、VS2017、VS2019、VS2022 等编译的程序。

旧系统支持‌:虽然 UCRT 是 Windows 10 的原生组件,但微软也为 Windows 7 SP1、Windows 8.1 和 Windows Server 2012 R2 提供了 UCRT 更新包。确保这些系统安装了最新的 Windows Update 或手动安装 KB2999226 等补丁,即可获得 UCRT 支持。

POSIX 与 Microsoft 特定函数‌:UCRT 支持许多 POSIX 函数,但为了符合 C 标准命名规范,Microsoft 推荐使用带下划线前缀的版本(如 _open 而非 open)。在使用 /std:c11 或 /std:c17 编译时,默认不暴露非标准的 POSIX 名称,需定义 _CRT_DECLARE_NONSTDC_NAMES 才能使用。

4. 常见问题与解决

“缺少 api-ms-win-crt-xxx.dll” 错误‌:这通常意味着系统缺少 UCRT 组件。在 Windows 10/11 上,运行 Windows Update 即可修复。在 Windows 7/8.1 上,需要安装“通用 C 运行时”更新或最新的 VC++ Redistributable。

安全函数警告‌:UCRT 引入了更安全的函数变体(如 sprintf_s 替代 sprintf),以防止缓冲区溢出。编译器默认会对使用不安全函数的代码发出警告,可通过定义 _CRT_SECURE_NO_WARNINGS 来禁用这些警告(不推荐),或改用安全函数。

安装建议‌:对于普通用户,安装最新的 ‌Microsoft Visual C++ Redistributable AIO (All-In-One)‌ 合集包是最便捷的解决方案,它会自动检测并安装包括 UCRT 在内的所有必要运行时组件,解决因缺少 DLL 导致的软件或游戏启动失败问题。

5. 总结

Windows 通用 C 运行库 (UCRT) 是微软为解决长期存在的 C 运行时碎片化问题而推出的关键基础设施。它将标准 C 库功能固化在操作系统层面,使得基于 VS2015 及更高版本开发的应用程序具有更好的跨版本兼容性和更简单的部署需求。保持系统更新或安装最新的 VC++ Redistributable 是确保 UCRT 正常工作的最佳实践。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值