语言那点事,crt
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. |
To illustrate this, consider the following example:
- .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.
When linking DLLs with LIBC.LIB, be aware that if there is apossibility that such a DLL will be called by a multithreadedprogram, the DLL will not support multiple threads running in theDLL at the same time, which can cause major problems. If there is apossibility that the DLL will be called by multithreaded programs,be sure to link it with one of the libraries that supportmultithreaded programs (LIBCMT.LIB, CRTDLL.LIB orMSVCRT.LIB).
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. |
Microsoft Visual C++ 32-bit edition contains these three forms aswell, however, the CRT in a DLL is named MSVCRT.LIB. The DLL isredistributable. Its name depends on the version of VC++ (ieMSVCRT10.DLL or MSVCRT20.DLL). Note however, that MSVCRT10.DLL isnot supported on Win32s, while CRTDLL.LIB is supported on Win32s.MSVCRT20.DLL comes in two versions: one for WindowsNT and the other for Win32s.
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 |
This permits the C Run-time libraries to properly allocate andinitialize C Run-time data when a process or thread is attaching tothe DLL, to properly clean up C Run-time data when a process isdetaching from the DLL, and for global C++ objects in the DLL to beproperly constructed and destructed.
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:
Be sure to pass on to CRT_INIT() all of the parameters of the entrypoint; CRT_INIT() expects those parameters, so things may not workreliably if they are omitted (in particular, fdwReason is requiredto determine whether process initialization or termination isneeded). 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 DLL
For 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. |
To avoid these problems, link both the .EXE and DLL with CRTDLL.LIBor MSVCRT.LIB, which allows both the .EXE and DLL to use the commonset of functions and data contained within CRT in a DLL, and CRun-time data such as stream handles can then be shared by both the.EXE and DLL.
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. |
To illustrate this, consider the following example:
- .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.
When linking DLLs with LIBC.LIB, be aware that if there is apossibility that such a DLL will be called by a multithreadedprogram, the DLL will not support multiple threads running in theDLL at the same time, which can cause major problems. If there is apossibility that the DLL will be called by multithreaded programs,be sure to link it with one of the libraries that supportmultithreaded programs (LIBCMT.LIB, CRTDLL.LIB orMSVCRT.LIB).
语言那点事,crt的更多相关文章
- C语言的那些事
变量的存数类型: 1:静态变量:凡是在代码任何快之外声明的变量总是存储在静态内存内,也就是不属于堆栈的内存. 对于这类变量.你无法对它们制指定存储类型. 2:存储于堆栈中,称为自动变量.当程序执行到声 ...
- 关于自学C语言这件事
说在前面 为什么会写下这篇文章?本人也并不是什么大神,但是也自学了很久,从一个小白开始自学,期间走过很多弯路,掉过许多坑.一路跌跌撞撞下来,算是有了点收获,有了点感想,将它们记录下来,希望能帮助新入坑 ...
- 页面切换语言包使用session不用cookie
cookie的问题,ifame中的cookie不一致 在父页面设置的语言包cookie,在iframe中获取不到.为什么呢? 为什么语言包这个事跟cookie过不去,有什么特殊的? iframe的sr ...
- 【C语言探索之旅】 开宗明义及第一课:什么是编程?
内容简介 1.课程大纲 2.第一部分第一课:什么是编程? 3.第一部分第二课预告:工欲善其事,必先利其器 课程大纲 不知道为什么,一直对C语言有一种很深厚的“情怀”(类似老罗对锤子手机的那种),说 ...
- 张金禹 C语言--第0次作业
1:在填报专业的时候,我也犹豫了很久,但最后还是选择了计算机专业.因为在上大学之前我就对编程.设计等有浓厚的兴趣,但繁重的高中学习任务使我没有过多的去关注,所以我选择了计算机专业去培养我在这方面的兴趣 ...
- C语言|博客作业12—学期总结
一.我学到的内容 二.我的收获 (1)https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/7603 收获:第一次接触C语言和写博客,感觉特别 ...
- c++interview
出自:https://github.com/huihut/interview Github | Docsify 简体中文 | English 关于 本仓库是面向 C/C++ 技 ...
- ABP文档 - 本地化
文档目录 本节内容: 简介 应用语言 本地化源 XML文件 注册XML本地化源 JSOn文件 注册JSON本地化源 资源文件 自定义源 获取一个本地文本 在服务端 在MVc控制器里 在MVC视图里 在 ...
- mysql 行锁
在电子商务里,经常会出现库存数量少,购买的人又特别多,大并发情况下如何确保商品数量不会被多次购买. 其实很简单,利用事务+for update就可以解决. 我们都知道for update实际上是共享锁 ...
随机推荐
- 服务器老是出现502 Bad Gateway?
给你的upstream增加keepalive配置没有? upstream backend { server http://127.0.0.1:8888; keepalive 32; } 在nginx和 ...
- GO语言的数据结构测试
用于docker了,go也慢慢看一些.. 推荐书籍<go语言实践>就是<Go in Action>的中文版,有文字版PDF的. package main import ( &q ...
- CF 862A Mahmoud and Ehab and the MEX【数组操作】
A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 megabytes in ...
- Web应用漏洞评估工具Paros
Web应用漏洞评估工具Paros Paros是Kali Linux集成的一款Web应用漏洞评估工具.该工具提供HTTP会话分析.网络爬虫.漏洞扫描三大功能.首先借助HTTP代理模式,该工具可以实时 ...
- [Codeforces 32E] Hide-and-Seek
Brief Intro: 给两个人的坐标,一堵墙和一面镜子,询问两人能否看见对方 Solution: 一道以分类讨论为主的计算几何题, 分别讨论两人坐标连线是否经过墙/镜子即可, 难点在于如何求出点x ...
- 1.1(java学习笔记) 面向过程与面向对象
面向过程思考时,我们会先思考具体的步骤,第一步走什么,第二步做什么. 比如电脑新建文件夹,第一步:打开电脑 第二步:按下鼠标右键. 第三步:找到新建选项 第四步:点击新建选项下的文件夹 c语言是典型的 ...
- Java高级架构师(一)第08节:基本业务功能和数据字典
- apk打包
1.在导航栏中选择Builder->Generate Signed Apk 2.新建点击Creat new... 3.注意路径后面写apk的名字(这个名字将会显示在手机软件的下方)
- 【spring data jpa】 spring data jpa 中 时间格式设置between and 查询
实例代码: //举报时间 Date createDate = entity.getCreateDate(); if (createDate != null){ predicates.add(cb.be ...
- 关于spring-data-jpa的排序问题
本测试基于springBoot框架实现. pom.xml文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...