C语言标准(不管是ANSI 还是ISO)包含2部分,一部分是语言本身的标准,另一部分是C标准函数库。C标准函数库规定了函数的原型和功能,但是并没限定这些函数要怎么实现。所谓满足标准C规定的C编译器,不仅指这个编译器满足C语言本身的标准,还指这个编译器提供了一组满足C标准库的库函数。这组库函数是由编译器厂商实现,并且满足标准C规定的功能和接口的。这些库函数,厂商并不一定要提供给用户源文件给用户编译用,可以是二进制目标文件给用户链接用(我猜这就是叫着运行库的原因之一,你看不到源码,能看到运行是的汇编码)。
所以说,不管是哪个厂商的CRT,只要他宣称他是标准的C编译器,那他的CRT就肯定是满足C标准中关于C标准库的规定。厂商除了还可以对自己的CRT进行扩展,因此,是CRT中包含了标准C库,而不是C库中包含了CRT。
同时还应该说明,C库函数里的有些函数需要操作系统支持才能实现的,比如printf和scanf函数等,这些函数管理输入和输出,但实际函数的底层是调用了操作系统提供的接口函数(实际就是系统调用)和实际的输入输出设备打交道(我猜这是叫C运行库的另外一个原因,它的某些功能要运行在操作系统的支持之上)。
大概你又要迷惑为什么又牵涉进来操作系统了。简单的讲,操作系统就是管理计算机软硬件资源,并为上层应用提供统一系统调用接口的一组程序。操作系统为我们管理千差万别的硬件,并为上层(这里是CRT)提供统一的系统调用接口。举例来说:输入设备千差万别:有串口,鼠标,键盘,触摸屏等等,由操作系统管理这些输入设备的差别,然后提供给scanf提供一个系统调用来得容易还是由C标准自己规定遇到每种不同的输入设备应该怎么动作来得容易?
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 编译器的出产商/个体/群体都顺从老的传统,在不同平台上都有相对应的 StandardLibrary,但大局部告终都是与各个平台有关的。由于各个 C 编译器对 C 的扶持和会意有许多抵触和精深的差异,因而就有了 ANSIC;ANSI C (主观愿望上)翔实的法定了 C 语言各个要素的翔实含义和编译器告终要求,引进了新的函数声明措施,同时订立了 StandardLibrary 的规范形式。因而C运行时库由编译器出产商供给。至于由其他厂商/个人/群体供给的头文件和库函数,该当称为第三方 C运行库(Third party C run-time libraries)。
4)C run-time library里面含有初始化代码,还有讹谬处理代码(例如divide byzero处理)。你写的过程能够未曾math库,过程照样运行,只是不能处理混杂的数学计算,不过万一未曾了Crun-time库,main()就不会被调用,exit()也不能被响应。因为C run-timelibrary包括了C过程运行的最大约和最常用的函数。
5)到了 C++ 世界里,有另外一个观念:Standard C++ Library,贝尔莱德蒸汽挂烫机它包括了上面所说的 C run-timelibrary 和 STL。包括 C run-time library 的起因很显明,C++ 是 C 的超集,没合原因再重新来一个 C++run-time library. VC针对C++ 加入的Standard C++ Library重要包括ar.artboard.net.cn: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或Importlibrary 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, clickSettings on the Project menu. Then click the C/C++ tab, and clickCode Generation in the Category box. See the Use Run-Time Librarydrop-down box.
从过程可移植性琢磨,万一两函数都可告终一种功能,选运行时库函数好,因为各个 C 编译器的出产商对规范C Run-timelibrary供给了统一的扶持.他倒感受像C++这么的语言的OO,纯是添乱。
When mixing library types adhere to the following:
• | CRT file handles may only be operated on by the CRT module thatcreated them. |
• | CRT FILE* pointers may only be operated on by the CRT modulethat created them. |
• | Memory allocated with the CRT function malloc() may only befreed or reallocated by the CRT module that allocated it. |
- .EXE is linked with MSVCRT.LIB - DLL A is linked with LIBCMT.LIB - DLL B is linked with CRTDLL.LIB
If DLL A allocates a block of memory with malloc(), only DLL A maycall free(), _expand(), or realloc() to operate on that block. Youcannot call malloc() from DLL A and try to free that block from the.EXE or from DLL B.
NOTE: If all three modules were linked withCRTDLL.LIB or all three were linked with MSVCRT.LIb, theserestrictions would not apply.
Section 1: Three Forms of C Run-Time (CRT)Libraries Are Available
There are three forms of the C Run-time library provided with theWin32 SDK:• | LIBC.LIB is a statically linked library for single-threadedprograms. |
• | LIBCMT.LIB is a statically linked library that supportsmultithreaded programs. |
• | CRTDLL.LIB is an import library for CRTDLL.DLL that alsosupports multithreaded programs. CRTDLL.DLL itself is part ofWindows NT. |
Section 2: Using the CRT Libraries WhenBuilding a DLL
When building a DLL which uses any of the C Run-time libraries, inorder to ensure that the CRT is properly initialized, either
1. | the initialization function must be named DllMain() and theentry point must be specified with the linker option-entry:_DllMainCRTStartup@12 - or - |
2. | the DLL's entry point must explicitly call CRT_INIT() onprocess attach and process detach |
The Win32 SDK samples all use the first method. Use them as anexample. Also refer to the Win32 Programmer's Reference forDllEntryPoint() and the Visual C++ documentation for DllMain().Note that DllMainCRTStartup() calls CRT_INIT() and CRT_INIT() willcall your application's DllMain(), if it exists.
If you wish to use the second method and call the CRTinitialization code yourself, instead of using DllMainCRTStartup()and DllMain(), there are two techniques:
1. | if there is no entry function which performs initializationcode, simply specify CRT_INIT() as the entry point of the DLL.Assuming that you've included NTWIN32.MAK, which defines DLLENTRYas "@12", add the following option to the DLL's linkline:
-entry:_CRT_INIT$(DLLENTRY)
- or - |
||||||||
2. | if you *do* have your own DLL entry point, do the following inthe entry point:
Below is a skeleton sample entry point function that shows when andhow to make these calls to CRT_INIT() in the DLL entrypoint:
NOTE that this is *not* necessary ifyou are using DllMain() and-entry:_DllMainCRTStartup@12. |
Section 3: Using NTWIN32.MAK to Simplify theBuild Process
There are macros defined in NTWIN32.MAK that can be used tosimplify your makefiles and to ensure that they are properly builtto avoid conflicts. For this reason, Microsoft highly recommendsusing NTWIN32.MAK and the macros therein.For compilation, use:
$(cvarsdll) for apps/DLLs using CRT in a DLLFor linking, use one of the following:
$(conlibsdll) for console apps/DLLs using CRT in a DLL $(guilibsdll) for GUI apps using CRT in a DLL
Section 4: Problems Encountered When UsingMultiple CRT Libraries
If an application that makes C Run-time calls links to a DLL thatalso makes C Run-time calls, be aware that if they are both linkedwith one of the statically-linked C Run-time libraries (LIBC.LIB orLIBCMT.LIB), the .EXE and DLL will have separate copies of all CRun-time functions and global variables. This means that C Run-timedata cannot be shared between the .EXE and the DLL. Some of theproblems that can occur as a result are:• | Passing buffered stream handles from the .EXE/DLL to the othermodule |
• | Allocating memory with a C Run-time call in the .EXE/DLL andreallocating or freeing it in the other module |
• | Checking or setting the value of the global errno variable inthe .EXE/DLL and expecting it to be the same in the other module. Arelated problem is calling perror() in the opposite module fromwhere the C Run- time error occurred, since perror() useserrno. |
Section 5: Mixing Library Types
You can link your DLL with CRTDLL.LIB/MSVCRT.LIB regardless of whatyour .EXE is linked with if you avoid mixing CRT data structuresand passing CRT file handles or CRT FILE* pointers to othermodules.When mixing library types adhere to the following:
• | CRT file handles may only be operated on by the CRT module thatcreated them. |
• | CRT FILE* pointers may only be operated on by the CRT modulethat created them. |
• | Memory allocated with the CRT function malloc() may only befreed or reallocated by the CRT module that allocated it. |
- .EXE is linked with MSVCRT.LIB - DLL A is linked with LIBCMT.LIB - DLL B is linked with CRTDLL.LIB
If DLL A allocates a block of memory with malloc(), only DLL A maycall free(), _expand(), or realloc() to operate on that block. Youcannot call malloc() from DLL A and try to free that block from the.EXE or from DLL B.
NOTE: If all three modules were linked withCRTDLL.LIB or all three were linked with MSVCRT.LIb, theserestrictions would not apply.