先转篇议论
注: 以下内容部分引自CSND中相关讨论的帖子,并结合自己的理解整理而成。仅供参考。
1)运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数.
2)C 语言是所谓的“小内核”语言,就其语言本身来说很小(不多的关键字,程序流程控制,数据类型等);所以,C 语言内核开发出来之后,Dennis Ritchie 和 Brian Kernighan 就用 C 本身重写了 90% 以上的 UNIX 系统函数,并且把其中最常用的部分独立出来,形成头文件和对应的 LIBRARY,C run-time library 就是这样形成的。
3)随后,随着 C 语言的流行,各个 C 编译器的生产商/个体/团体都遵循老的传统,在不同平台上都有相对应的 Standard Library,但大部分实现都是与各个平台有关的。由于各个 C 编译器对 C 的支持和理解有很多分歧和微妙的差别,所以就有了 ANSI C;ANSI C (主观意图上)详细的规定了 C 语言各个要素的具体含义和编译器实现要求,引进了新的函数声明方式,同时订立了 Standard Library 的标准形式。所以C运行时库由编译器生产商提供。至于由其他厂商/个人/团体提供的头文件和库函数,应当称为第三方 C 运行库(Third party C run-time libraries)。
4)C run-time library里面含有初始化代码,还有错误处理代码(例如divide by zero处理)。你写的程序可以没有math库,程序照样运行,只是不能处理复杂的数学运算,不过如果没有了C run-time库,main()就不会被调用,exit()也不能被响应。因为C run-time library包含了C程序运行的最基本和最常用的函数。
5)到了 C++ 世界里,有另外一个概念:Standard C++ Library,它包括了上面所说的 C run-time library 和 STL。包含 C run-time library 的原因很明显,C++ 是 C 的超集,没有理由再重新来一个 C++ run-time library. VC针对C++ 加入的Standard C++ Library主要包括:LIBCP.LIB, LIBCPMT.LIB和 MSVCPRT.LIB
6)Windows环境下,VC提供的 C run-time library又分为动态运行时库和静态运行时库。
动态运行时库主要是DLL库文件msvcrt.dll(or MSVCRTD.DLL for debug build),对应的Import library文件是MSVCRT.LIB(MSVCRTD.LIB for debug build)
静态运行时库(release版)对应的主要文件是:
LIBC.LIB (Single thread static library, retail version)
LIBCMT.LIB (Multithread static library, retail version)
msvcrt.dll提供几千个C函数,即使是像printf这么低级的函数都在msvcrt.dll里。其实你的程序运行时,很大一部分时间时在这些运行库里运行。在你的程序(release版)被编译时,VC会根据你的编译选项(单线程、多线程或DLL)自动将相应的运行时库文件(libc.lib,libcmt.lib或Import library msvcrt.lib)链接进来。
编译时到底哪个C run-time library联入你的程序取决于编译选项:
/MD, /ML, /MT, /LD (Use Run-Time Library)
你可以VC中通过以下方法设置选择哪个C run-time library联入你的程序:
To find these options in the development environment, click Settings on the Project menu. Then click the C/C++ tab, and click Code Generation in the Category box. See the Use Run-Time Library drop-down box.
从程序可移植性考虑,如果两函数都可完成一种功能,选运行时库函数好,因为各个 C 编译器的生产商对标准C Run-time library提供了统一的支持.
以下来自MSDN
C Run-Time Libraries
The following table lists the release versions of the C run-time library files, along with their associated compiler options and environment variables. Prior to Visual C++ 4.2, the C run-time libraries contained the iostream library functions. In Visual C++ 4.2 and later, the old iostream library functions have been removed from LIBC.LIB, LIBCMT.LIB, and MSVCRT.LIB. (This change was made because the Standard C++ library has been added to Visual C++, and it contains a new set of iostream libraries. Thus, two sets of iostream functions are now included in Visual C++.) The old iostream functions now exist in their own libraries: LIBCI.LIB, LIBCIMT.LIB, and MSVCIRT.LIB. The new iostream functions, as well as many other new functions, exist in the Standard C++ libraries: LIBCP.LIB, LIBCPMT.LIB, and MSVCPRT.LIB.
The Standard C++ library and the old iostream library are incompatible, and only one of them can be linked with your project. See Port to the Standard C++ Library and theStandard C++ Library Overview for details.
When you build a release version of your project, one of the basic C run-time libraries (LIBC.LIB, LIBCMT.LIB, and MSVCRT.LIB) is linked by default, depending on the compiler option you choose (single-threaded, multithreaded, or DLL). Depending on the headers you use in your code, a library from the Standard C++ libraries or one from the old iostream libraries may also be linked:
- If you include aStandard C++ library header in your code, a Standard C++ library will be linked in automatically by Visual C++ at compile time. For example:
#include <ios>
- If you include an old iostream library header, an old iostream library will be linked in automatically by Visual C++ at compile time. For example:
#include <ios.h>
Note that headers from the Standard C++ library and the old iostream library cannot be mixed.
Headers determine whether a Standard C++ library, an old iostream library, or neither will be linked. Compiler options determine which of the libraries to be linked is the default (single-threaded, multithreaded, or DLL). When a specific library compiler option is defined, that library is considered to be the default and its environment variables are automatically defined.
C Run-Time Library (without iostream) | Characteristics | Option | Defined |
LIBC.LIB | Single threaded, static link | /ML | |
LIBCMT.LIB | Multithreaded, static link | /MT | _MT |
MSVCRT.LIB | Multithreaded, dynamic link (import library for MSVCRT.DLL) | /MD | _MT, _DLL |
Standard C++ Library | Characteristics | Option | Defined |
LIBCP.LIB | Single threaded, static link | /ML | |
LIBCPMT.LIB | Multithreaded, static link | /MT | _MT |
MSVCPRT.LIB | Multithreaded, dynamic link (import library for MSVCRT.DLL) | /MD | _MT, _DLL |
Old Iostream Library | Characteristics | Option | Defined |
LIBCI.LIB | Single threaded, static link | /ML | |
LIBCIMT.LIB | Multithreaded, static link | /MT | _MT |
MSVCIRT.LIB | Multithreaded, dynamic link (import library for MSVCIRT.DLL) | /MD | _MT, _DLL |
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 C Run-Time Debug Libraries.
<script language="JavaScript" src="MS-ITS:dsmsdn.chm::/html/msdn_footer.js" type="text/javascript"></script>