运行时库的理解

1.什么是运行时库

运行时库(Runtime Library)是程序运行时所需要的库文件.

它把具有语言特性(language feature)的函数放到了库函数中.例如C运行时库中包含了fopen(), strcpy()这些非核心语言的特性.

For example, the C runtime library is a library containing things like fopen() for opening files or strcpy() for copying strings. While the compiler understands the core language (ifwhile and so on, including how to call functions), the non-core-language features are provided as libraries containing functions that can be called.


在Windows下通常为lib或者dll的文件,比如libcmt.lib, msvcrt.lib+msvcrtxx.dll.如果你安装了VS2010,在安装目录下的VCcrtsrc下有运行时库(CRT)的源代码,这里既有C的文件(如output.c、stdio.h等),也有C++的文件(如iostream、string)。

2.运行时库的作用

(1)提供C标准库或者C++标准库.

(2)为程序提供启动函数.启动函数主要进行一些初始化操作:比如初始化全局变量,加载用户程序的入口函数

void mainCRTStartup(void)
{
 int mainret;
 /*获得WIN32完整的版本信息*/
 _osver = GetVersion();
 _winminor = (_osver >> 8) & 0x00FF ;
 _winmajor = _osver & 0x00FF ;
 _winver = (_winmajor << 8) + _winminor;
 _osver = (_osver >> 16) & 0x00FFFF ;
 _ioinit(); /* initialize lowio */
 /* 获得命令行信息 */
 _acmdln = (char *) GetCommandLineA();
 /* 获得环境信息 */
 _aenvptr = (char *) __crtGetEnvironmentStringsA();
 _setargv(); /* 设置命令行参数 */
 _setenvp(); /* 设置环境参数 */
 _cinit(); /* C数据初始化:全局变量初始化,就在这里!*/
 __initenv = _environ;
 mainret = main( __argc, __argv, _environ ); /*调用main函数*/
 exit( mainret );
}

3.VS运行时库的几种类型

 其中/MD,/MDd(动态库)是主流选择,即程序运行时通过动态链接的方式来加载,这样内存中只需要保留一份库文件.

注意使用运行时库时,Debug模式下使用Debug版本的,Release模式下使用Release版本的.

————————————————

参考链接:

1.https://stackoverflow.com/questions/25598098/runtime-library-vs-dynamic-library

2.https://blog.csdn.net/luoweifu/article/details/49055933

新战场:https://blog.csdn.net/Stephen___Qin
原文地址:https://www.cnblogs.com/Stephen-Qin/p/13048565.html