`
webcode
  • 浏览: 5929378 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

C runtime Library And Standard C++ Library

阅读更多

VisualC 中的C运行时库浅析

 1.概论

  运行时库是程序在运行时所需要的库文件,通常运行时库是以LIBDLL形式提供的。C运行时库诞生于20世纪70年代,当时的程序世界还很单纯,应用程序都是单线程的,多任务或多线程机制在此时还属于新观念。所以这个时期的C运行时库都是单线程的。

  随着操作系统多线程技术的发展,最初的C运行时库无法满足程序的需求,出现了严重的问题。C运行时库使用了多个全局变量(例如errno)和静态变量,这可能在多线程程序中引起冲突。假设两个线程都同时设置errno,其结果是后设置的errno会将先前的覆盖,用户得不到正确的错误信息。

  因此,Visual C 提供了两种版本的C运行时库。一个版本供单线程应用程序调用,另一个版本供多线程应用程序调用。多线程运行时库与单线程运行时库有两个重大差别:

  (1)类似errno的全局变量,每个线程单独设置一个;

  这样从每个线程中可以获取正确的错误信息。

  (2)多线程库中的数据结构以同步机制加以保护。
.

  这样可以避免访问时候的冲突。

  Visual C 提供的多线程运行时库又分为静态链接库和动态链接库两类,而每一类运行时库又可再分为debug版和release版,因此Visual C 共提供了6个运行时库。如下表:

C
运行时库库文件 Single thread(static link) libc.lib Debug single thread(static link) libcd.lib MultiThread(static link) libcmt.lib Debug multiThread(static link) libcmtd.lib MultiThread(dynamic link) msvert.lib Debug multiThread(dynamic link) msvertd.lib
  2.C运行时库的作用

  C运行时库除了给我们提供必要的库函数调用(如memcpyprintfmalloc等)之外,它提供的另一个最重要的功能是为应用程序添加启动函数。

  C运行时库启动函数的主要功能为进行程序的初始化,对全局变量进行赋初值,加载用户程序的入口函数。

  不采用宽字符集的控制台程序的入口点为mainCRTStartup(void)。下面我们以该函数为例来分析运行时库究竟为我们添加了怎样的入口程序。这个函数在crt0.c中被定义,下列的代码经过了笔者的整理和简化:

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 );
}
  从以上代码可知,运行库在调用用户程序的mainWinMain函数之前,进行了一些初始化工作。初始化完成后,接着才调用了我们编写的mainWinMain函数。只有这样,我们的C语言运行时库和应用程序才能正常地工作起来。

  除了crt0.c外,C运行时库中还包含wcrt0.c wincrt0.cwwincrt0.c三个文件用来提供初始化函数。wcrt0.ccrt0.c的宽字符集版,wincrt0.c中包含windows应用程序的入口函数,而wwincrt0.c则是wincrt0.c的宽字符集版。
根据专家观察,这样的理论和现象都是值得各位站长深思的,所以希望大家多做研究学习,争取总结出更多更好的经验!

  Visual C 的运行时库源代码缺省情况下不被安装。如果您想查看其源代码,则需要重装Visual C ,并在重装在时选中安装运行库源代码选项。

3.各种C运行时库的区别

  (1)静态链接的单线程库

  静态链接的单线程库只能用于单线程的应用程序,C运行时库的目标代码最终被编译在应用程序的二进制文件中。通过/ML编译选项可以设置Visual C 使用静态链接的单线程库。

  (2)静态链接的多线程库

  静态链接的多线程库的目标代码也最终被编译在应用程序的二进制文件中,但是它可以在多线程程序中使用。通过/MD编译选项可以设置Visual C 使用静态链接的单线程库。

  (3)动态链接的运行时库

  动态链接的运行时库将所有的C库函数保存在一个单独的动态链接库MSVCRTxx.DLL中,MSVCRTxx.DLL处理了多线程问题。使用/ML编译选项可以设置Visual C 使用动态链接的运行时库。

  /MDd /MLd /MTd 选项使用 Debug runtime library(调试版本的运行时刻函数库),与/MD /ML /MT分别对应。Debug版本的 Runtime Library 包含了调试信息,并采用了一些保护机制以帮助发现错误,加强了对错误的检测,因此在运行性能方面比不上Release版本。

  下面看一个未正确使用C运行时库的控制台程序:

#include <stdio.h>
#include <afx.h>
int main()
{
 CFile file;
 CString str("I love you");
 TRY
 {
  file.Open("file.dat",CFile::modeWrite | CFile::modeCreate);
 }
 CATCH( CFileException, e )
 {
  #ifdef _DEBUG
  afxDump << "File could not be opened " << e->m_cause << "\n";
  #endif
 }
 END_CATCH

 file.Write(str,str.GetLength());
 file.Close();
}
  我们在"rebuild all"的时候发生了link错误:
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
main.exe : fatal error LNK1120: 2 unresolved externals
Error executing cl.exe.
  发生错误的原因在于Visual C 对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程。我们只需要在Visual C 6.0中依次点选Project->Settings->C/C 菜单和选项,在Project Options里修改编译选项即可。

1)运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数.

2)C 语言是所谓的小内核语言,就其语言本身来说很小(不多的关键字,程序流程控制,数据类型等);所以,C 语言内核开发出来之后,Dennis Ritchie Brian Kernighan 就用 C 本身重写了 90% 以上的 UNIX 系统函数,并且把其中最常用的部分独立出来,形成头文件和对应的 LIBRARYC run-time library 就是这样形成的。

3)随后,随着 C 语言的流行,各个 C 编译器的生产商/个体/团体都遵循老的传统,在不同平台上都有相对应的 Standard Library,但大部分实现都是与各个平台有关的。由于各个 C 编译器对 C 的支持和理解有很多分歧和微妙的差别,所以就有了 ANSI CANSI 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.libImport 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提供了统一的支持.

分享到:
评论

相关推荐

    IBM XL C/C++ for AIX, V11.1 Standard C++ Library Reference

    C++标准库参考文档,包含C++标准库的基本介绍和详尽介绍

    C++标准库(第二版)英文版.pdf

    The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...

    THE BOOST C++ METAPROGRAMMING LIBRARY

    equivalent - the Standard Template Library (STL), a part of the C++ standard library [STL94], [ISO98]. Like the STL, it defines an open conceptual and implementation framework which can serve as a ...

    C++标准(Standard for Programming Language C++)

    17.2 The C standard library 17.3 Definitions 17.4 Additional definitions 17.5 Method of description (Informative) 17.6 Library-wide requirements 18 Language support library 18.1 General 18.2 ...

    C++程序设计语言特别版

    This book introduces standard C++† and the key programming and design techniques supported by C++. Standard C++ is a far more powerful and polished language than the version of C++ introduced by the ...

    Google C++ International Standard.pdf

    7 Standard conversions 86 7.1 Lvalue-to-rvalue conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 7.2 Array-to-pointer conversion . . . . . . . . . . . . . . . . . . ....

    Addison.Wesley.C++.Template.Metaprogramming.LiB.chm

    The companion CD-ROM contains all Boost C++ libraries, including the Boost Metaprogramming Library and its reference documentation, along with all of the book's sample code and extensive supplementary...

    C in a Nutshell 2nd 原版pdf by Prinz & Crawford

    This book is a complete reference to the C programming language and the C runtime library. As an “In a Nutshell” book, its purpose is to serve as a convenient, reliable companion for C programmers ...

    Practical C++ Programming C++编程实践

    From C to C++ K&R-Style Functions struct malloc and free Turning Structures into Classes setjmp and longjmp Mixing C and C++ Code Summary Programming Exercise 29. C++’s Dustier Corners do/while goto...

    Oracle.Data.Access.Components.v6.90.0.53.Full.Source.D5-2010

    The OCI provides a library of standard database access and retrieval functions in the form of a dynamic runtime library, ORA*.DLL, that can be linked in by the application. Using BDE in Oracle ...

    Oracle Data Access Components v6.90.0.57 (完整源码)

    The OCI provides a library of standard database access and retrieval functions in the form of a dynamic runtime library, ORA*.DLL, that can be linked in by the application. Using BDE in Oracle ...

    电脑游戏及系统必备运行库

    它包括了STL(Standard Template Library,标准模板库)、RTL(Runtime Library,运行时库)和DLL(Dynamic Link Library,动态链接库)等组件。C++运行时库为C++程序提供了内存管理、异常处理、多线程等关键功能。...

    Turbo C++ 3.0[DISK]

    For the latest information about Turbo C++ and its accompanying programs and manuals, read this file in its entirety. TABLE OF CONTENTS ----------------- 1. How to Get Help 2. Installation 3. ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    link ▶Use standard order for readability and to avoid hidden dependencies: C library, C++ library, other libraries' .h, your project's .h. All of a project's header files should be listed as ...

    Developing Applications With Visual Studio.Net

    The .NET Framework supplies programmers with rich standard run-time services, supports the development of Web-based services, and provides both inter-language and inter-machine interoperability....

    ODAC 5.1.0.6

    The OCI provides a library of standard database access and retrieval functions in the form of a dynamic runtime library, ORA*.DLL, that can be linked in by the application. Using BDE in Oracle ...

    RxLib控件包内含RxGIF,全部源码及DEMO

    This version is the result of long unactivity of RX Library authors and some imperfections and bugs of other RX adaptations to Delphi 6. The authors of this version disclaim all warranties as to ...

    Turbo C++ 3.00[DISK]

    For the latest information about Turbo C++ and its accompanying programs and manuals, read this file in its entirety. TABLE OF CONTENTS ----------------- 1. How to Get Help 2. Installation 3. ...

    glibc 2.22

    The GNU C Library is the standard system C library for all GNU systems, and is an important part of what makes up a GNU system. It provides the system API for all programs written in C and C-...

    SQLMemTable for Delphi / C++ Builder

    Target Environment: Delphi 4, 5, 6, 7 and C++ Builder 4, 5, 6.Company information-------------------Company Name: AidAim SoftwareContact E-mail Address: support@aidaim.comContact ...

Global site tag (gtag.js) - Google Analytics