日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

语言那点事,crt

發(fā)布時(shí)間:2024/9/20 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 语言那点事,crt 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

C語(yǔ)言標(biāo)準(zhǔn)(不管是ANSI 還是ISO)包含2部分,一部分是語(yǔ)言本身的標(biāo)準(zhǔn),另一部分是C標(biāo)準(zhǔn)函數(shù)庫(kù)。C標(biāo)準(zhǔn)函數(shù)庫(kù)規(guī)定了函數(shù)的原型和功能,但是并沒(méi)限定這些函數(shù)要怎么實(shí)現(xiàn)。所謂滿足標(biāo)準(zhǔn)C規(guī)定的C編譯器,不僅指這個(gè)編譯器滿足C語(yǔ)言本身的標(biāo)準(zhǔn),還指這個(gè)編譯器提供了一組滿足C標(biāo)準(zhǔn)庫(kù)的庫(kù)函數(shù)。這組庫(kù)函數(shù)是由編譯器廠商實(shí)現(xiàn),并且滿足標(biāo)準(zhǔn)C規(guī)定的功能和接口的。這些庫(kù)函數(shù),廠商并不一定要提供給用戶源文件給用戶編譯用,可以是二進(jìn)制目標(biāo)文件給用戶鏈接用(我猜這就是叫著運(yùn)行庫(kù)的原因之一,你看不到源碼,能看到運(yùn)行是的匯編碼)。
所以說(shuō),不管是哪個(gè)廠商的CRT,只要他宣稱他是標(biāo)準(zhǔn)的C編譯器,那他的CRT就肯定是滿足C標(biāo)準(zhǔn)中關(guān)于C標(biāo)準(zhǔn)庫(kù)的規(guī)定。廠商除了還可以對(duì)自己的CRT進(jìn)行擴(kuò)展,因此,是CRT中包含了標(biāo)準(zhǔn)C庫(kù),而不是C庫(kù)中包含了CRT。

同時(shí)還應(yīng)該說(shuō)明,C庫(kù)函數(shù)里的有些函數(shù)需要操作系統(tǒng)支持才能實(shí)現(xiàn)的,比如printf和scanf函數(shù)等,這些函數(shù)管理輸入和輸出,但實(shí)際函數(shù)的底層是調(diào)用了操作系統(tǒng)提供的接口函數(shù)(實(shí)際就是系統(tǒng)調(diào)用)和實(shí)際的輸入輸出設(shè)備打交道(我猜這是叫C運(yùn)行庫(kù)的另外一個(gè)原因,它的某些功能要運(yùn)行在操作系統(tǒng)的支持之上)。

大概你又要迷惑為什么又牽涉進(jìn)來(lái)操作系統(tǒng)了。簡(jiǎn)單的講,操作系統(tǒng)就是管理計(jì)算機(jī)軟硬件資源,并為上層應(yīng)用提供統(tǒng)一系統(tǒng)調(diào)用接口的一組程序。操作系統(tǒng)為我們管理千差萬(wàn)別的硬件,并為上層(這里是CRT)提供統(tǒng)一的系統(tǒng)調(diào)用接口。舉例來(lái)說(shuō):輸入設(shè)備千差萬(wàn)別:有串口,鼠標(biāo),鍵盤,觸摸屏等等,由操作系統(tǒng)管理這些輸入設(shè)備的差別,然后提供給scanf提供一個(gè)系統(tǒng)調(diào)用來(lái)得容易還是由C標(biāo)準(zhǔn)自己規(guī)定遇到每種不同的輸入設(shè)備應(yīng)該怎么動(dòng)作來(lái)得容易?

1)運(yùn)行時(shí)庫(kù)即便 C run-time library,是 C 而非 C++ 語(yǔ)言世界的觀念:取這個(gè)名字即便因?yàn)槟愕?C 過(guò)程運(yùn)行時(shí)必需這些庫(kù)中的函數(shù).

2)C 語(yǔ)言是所謂的“小內(nèi)核”語(yǔ)言,就其語(yǔ)言本身來(lái)說(shuō)很小(不多的關(guān)鍵字,過(guò)程流程扼制,數(shù)據(jù)種類等);因而,C語(yǔ)言內(nèi)核開(kāi)發(fā)出來(lái)爾后,Dennis Ritchie 和 Brian Kernighan 就用 C 本身重寫了 90% 以上的 UNIX系統(tǒng)函數(shù),并且把其中最常用的局部自力更生出來(lái),構(gòu)成頭文件和對(duì)應(yīng)的 LIBRARY,C run-time library 即便這么構(gòu)成的。

3)隨后,隨著 C 語(yǔ)言的流行,各個(gè) C 編譯器的出產(chǎn)商/個(gè)體/群體都順從老的傳統(tǒng),在不同平臺(tái)上都有相對(duì)應(yīng)的 StandardLibrary,但大局部告終都是與各個(gè)平臺(tái)有關(guān)的。由于各個(gè) C 編譯器對(duì) C 的扶持和會(huì)意有許多抵觸和精深的差異,因而就有了 ANSIC;ANSI C (主觀愿望上)翔實(shí)的法定了 C 語(yǔ)言各個(gè)要素的翔實(shí)含義和編譯器告終要求,引進(jìn)了新的函數(shù)聲明措施,同時(shí)訂立了 StandardLibrary 的規(guī)范形式。因而C運(yùn)行時(shí)庫(kù)由編譯器出產(chǎn)商供給。至于由其他廠商/個(gè)人/群體供給的頭文件和庫(kù)函數(shù),該當(dāng)稱為第三方 C運(yùn)行庫(kù)(Third party C run-time libraries)。

4)C run-time library里面含有初始化代碼,還有訛謬處理代碼(例如divide byzero處理)。你寫的過(guò)程能夠未曾math庫(kù),過(guò)程照樣運(yùn)行,只是不能處理混雜的數(shù)學(xué)計(jì)算,不過(guò)萬(wàn)一未曾了Crun-time庫(kù),main()就不會(huì)被調(diào)用,exit()也不能被響應(yīng)。因?yàn)镃 run-timelibrary包括了C過(guò)程運(yùn)行的最大約和最常用的函數(shù)。


5)到了 C++ 世界里,有另外一個(gè)觀念:Standard C++ Library,貝爾萊德蒸汽掛燙機(jī)它包括了上面所說(shuō)的 C run-timelibrary 和 STL。包括 C run-time library 的起因很顯明,C++ 是 C 的超集,沒(méi)合原因再重新來(lái)一個(gè) C++run-time library. VC針對(duì)C++ 加入的Standard C++ Library重要包括ar.artboard.net.cn:LIBCP.LIB,LIBCPMT.LIB和 MSVCPRT.LIB

6)Windows環(huán)境下,VC供給的 C run-time library又分為動(dòng)態(tài)運(yùn)行時(shí)庫(kù)和靜態(tài)運(yùn)行時(shí)庫(kù)。
動(dòng)態(tài)運(yùn)行時(shí)庫(kù)重要是DLL庫(kù)文件msvcrt.dll(or MSVCRTD.DLL for debug build),對(duì)應(yīng)的Import library文件是MSVCRT.LIB(MSVCRTD.LIB for debug build)
靜態(tài)運(yùn)行時(shí)庫(kù)(release版)對(duì)應(yīng)的重要文件是:
LIBC.LIB (Single thread static library, retail version)
LIBCMT.LIB (Multithread static library, retail version)

msvcrt.dll供給幾千個(gè)C函數(shù),即便是像printf這么低級(jí)的函數(shù)都在msvcrt.dll里。其實(shí)你的過(guò)程運(yùn)行時(shí),很大一局部工夫時(shí)在這些運(yùn)行庫(kù)里運(yùn)行。在你的過(guò)程(release版)被編譯時(shí),VC會(huì)依據(jù)你的編譯選項(xiàng)(單線程、多線程或DLL)積極將相應(yīng)的運(yùn)行時(shí)庫(kù)文件(libc.lib,libcmt.lib或Importlibrary msvcrt.lib)鏈接進(jìn)來(lái)。

編譯時(shí)究竟哪個(gè)C run-time library聯(lián)入你的過(guò)程取決于編譯選項(xiàng):
/MD, /ML, /MT, /LD (Use Run-Time Library)
你能夠VC中穿越以下措施設(shè)置抉擇哪個(gè)C run-time library聯(lián)入你的過(guò)程:
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.

從過(guò)程可移植性琢磨,萬(wàn)一兩函數(shù)都可告終一種功能,選運(yùn)行時(shí)庫(kù)函數(shù)好,因?yàn)楦鱾€(gè) C 編譯器的出產(chǎn)商對(duì)規(guī)范C Run-timelibrary供給了統(tǒng)一的扶持.他倒感受像C++這么的語(yǔ)言的OO,純是添亂。

一 C Run-TimeLibraries的三種可用形式 Win32 SDK提供了三種C Run-time library的可用形式 LIBC.LIB:單線程程序靜態(tài)鏈接到運(yùn)行時(shí)庫(kù) LIBCMT.LIB:多線程程序靜態(tài)鏈接到運(yùn)行時(shí)庫(kù) CRTDLL.LIB:CRTDLL.DLL的導(dǎo)入庫(kù),支持多線程程序的鏈接。CTRDLL.DLL 本身是Windows NT的一部分。 ms vc++32位 版本同時(shí)包含了這三種運(yùn)行時(shí)庫(kù),不過(guò)CRT 動(dòng)態(tài)庫(kù)被命名為MSVCRT.LIB。 這個(gè)動(dòng)態(tài)鏈接庫(kù)是可重新發(fā)布的。它的名字取決于vc++的版本(MSVCRT10.DLL 或者M(jìn)SVCRT20.DLL)。注意,win32不載支持MSVCRT10.DLL,但是支持CRTDLL.LIB;MSVCRT20.DLL有兩個(gè)版本,一個(gè)是windowsnt版,另一個(gè)是win32版。 二 當(dāng)生成一個(gè)DLL時(shí)使用CRTLibraries 在生成一個(gè)DLL時(shí),無(wú)論使用了哪個(gè)C Run-timelibraries庫(kù),都要保證所使用的CRT被正確的初始化。可以使用下面任何一種方法: 1 初始化函數(shù)必須被命名為:DllMain() 并且入庫(kù)點(diǎn)必須在連接器選項(xiàng)中必須指定入口點(diǎn)位:_DllMainCrtStartup@12-或者-。 2在附加進(jìn)程和分離進(jìn)程的過(guò)程中,這個(gè)DLL的入庫(kù)點(diǎn)必須顯式調(diào)用CRT_INIT()。  這樣當(dāng)一個(gè)進(jìn)程或者線程連接到這個(gè)DLL時(shí),C運(yùn)行時(shí)庫(kù)可以正確的分配和初始化數(shù)據(jù),并且當(dāng)進(jìn)程或者線程離開(kāi)DLl時(shí)候可以正確的清理掉運(yùn)行時(shí)庫(kù), DLL中的全局C++對(duì)象(globalC++ objects)也可以正確的構(gòu)造和解構(gòu)。 下面的Win32 SDK例子程序都使用了第一種方法。請(qǐng)參考 win32programmer's reference for DllentryPoint() and the Vc++documentation for DllMain(). 注意函數(shù)DllMainCRTStartup()調(diào)用了 CRT_INIT(), 而CRT_INIT()在退出前將調(diào)用你的程序的DllMain()。如果你希望使用第二種方法來(lái)在自己調(diào)用CRT初始化代碼,而不使用DllMainCRTStartup()和 DllMain()函數(shù),你可以使用兩種技術(shù): 1 如果沒(méi)有入口函數(shù)來(lái)執(zhí)行初始化代碼,只需要指定CRT_INIT()作為該DLL的入口點(diǎn)。假設(shè)你已經(jīng)包含了文件NTWIN#@.MAK,?這個(gè)文件定義DLLENTRY 為"@12",?加入以下選項(xiàng)到DLL的鏈接選項(xiàng)命令行: -entry:_CRT_INIT$(DLLENTRY) - or - 2 如果你確實(shí)想擁有自己的DLL入口點(diǎn),在入口點(diǎn)做如下的事情: a使用CRT_INIT()的原形: BOOLWINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD fdwReason, LPVOIDlpReserved) ; 關(guān)于CRT_INIT()的返回值,可以看 documentation DllEntryPoint,它返回了相同的返回值。 b 在DLL_PROCESS_ATTACH 和DLL_THREAD_ATTACH塊中, 在調(diào)用任何C 運(yùn)行時(shí)庫(kù)函數(shù)和對(duì)任何浮點(diǎn)指針操作前首先調(diào)用CRT_INIT()。 c 調(diào)用自己的進(jìn)程/線程 初始/結(jié)束代碼。 d 在DLL_PROCESS_DETACH和DLL_THREAD_DETACH中,在所有的C運(yùn)行時(shí)庫(kù)函數(shù)被調(diào)用,并且所有浮點(diǎn)指針操作完成以后,最后一次調(diào)用CRT_INIT()。 要確保向CRT_INIT()傳遞了入口點(diǎn)的所有參數(shù)。CRT_INIT()需要這些這些參數(shù)。如果參數(shù)沒(méi)有全部傳遞過(guò)去,可能不能穩(wěn)定運(yùn)行(尤其需要fdwReason參數(shù)來(lái)確定進(jìn)程的初始化或者結(jié)束)。下邊實(shí)例關(guān)于如何在入口點(diǎn)調(diào)用CRT_INIT()。 BOOL WINAPIDllEntryPoint(HINSTANCE hinstDll, DWORD fdwReason, LPVOIDlpReserved) { if (fdwReason ==DLL_PROCESS_ATTACH || fdwReason == DLL_THREAD_ATTACH) if(!_CRT_INIT(hinstDll, fdwReason, lpReserved)) return (FALSE) ; if(fdwReason==DLL_PROCESS_DETACH || fdwReason==DLL_THREAD_DETACH) if(!_CRT_INIT(hinstDLL, fdwReason, lpReserved)) return (FALSE) ; return (TRUE) ; } 當(dāng)然,如果使用DLLMain()并且設(shè)置了鏈接選項(xiàng):-entry:_DllMainCRTStartup@12,則上述代碼就不需要了。 三使用NTWIN32.MAK來(lái)簡(jiǎn)化生成過(guò)程 在NTWIN32.MAK中的宏定義可以幫助你簡(jiǎn)化makefiles,并且可以確保正確的生成而避免沖突產(chǎn)生。因此,ms強(qiáng)烈推薦使用NTWIN32.MAK以及它所包含的宏。 編譯使用: $(cvarsdll)??for apps/DLLs using CRT in a DLL 鏈接使用下列行中的一個(gè): $(conlibstdll)???forconsole apps/DLLs using CRT in a DLL $(guilibsdll)????for GUI apps using CRT in a DLL 四 當(dāng)使用Multiple CRT庫(kù)是可能遇到的問(wèn)題 如果一個(gè)使用了C運(yùn)行時(shí)調(diào)用的應(yīng)用程序,鏈接到另一個(gè)也使用了了C運(yùn)行時(shí)調(diào)用的DLL,注意如果他們都連接到了一個(gè)C運(yùn)行時(shí)的靜態(tài)庫(kù)(LIBC.LIB 或者 LIBCMT.LIB), 這個(gè)EXE和DLL將各自擁有C運(yùn)行時(shí)的單獨(dú)的拷貝和全局變量。這意味著C運(yùn)行時(shí)數(shù)據(jù)不能被這個(gè).EXE和.DLL所共享。當(dāng)進(jìn)行以下操作時(shí)會(huì)產(chǎn)生問(wèn)題: 1 從.EXE 或者.DLL傳遞緩沖流句柄(bufferd streamhandles)到另一個(gè)模塊 2 使用C運(yùn)行時(shí)庫(kù)在一個(gè)模塊中分配一塊內(nèi)存,而在另外一個(gè)模塊中對(duì)這個(gè)內(nèi)存進(jìn)行重分配操作或者是房?jī)?nèi)存操作(allocrealloc)。 3 在.EXE或者.DLL模塊中檢查或者設(shè)置一個(gè)全局錯(cuò)誤號(hào)變量(globalerrnovariable)的值,而期望在另外的模塊中會(huì)獲得同樣的值。另外一個(gè)相關(guān)的錯(cuò)誤操作是:在未產(chǎn)生錯(cuò)誤的模塊中調(diào)用perror()函數(shù)。(perror()會(huì)使用錯(cuò)誤號(hào)) 為了避免這些問(wèn)題,應(yīng)該把.EXE和.DLL都連接上 CRTDLL.LIB 或者 MSVCRT.LIB。這樣允許.EXE和.DLL都是使用在 動(dòng)態(tài)CRT中包含的公共函數(shù)集和數(shù)據(jù), 并且C運(yùn)行時(shí)數(shù)據(jù),例如流句柄,可以被.EXE和.DLL所共享。 五混合庫(kù)類型的裝入問(wèn)題(不懂,待譯。有懂得人請(qǐng)指教) You can link your DLL with CRTDLL.LIB/MSVCRT.LIB regardless ofwhat your .EXE is linked with if you avoid mixing CRT datastructures and passing CRT file handles or CRT FILE* pointers toother modules.

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 the .EXE creates a CRT file handle using _create() or_open(), this file handle may only be passed to _lseek(), _read(),_write(), _close(), etc. in the .EXE file. Do not pass this CRTfile handle to either DLL. Do not pass a CRT file handle obtainedfrom either DLL to the other DLL or to the .EXE.

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:

a.Use this prototype for CRT_INIT(): BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD fdwReason,LPVOID lpReserved); For information on CRT_INIT() return values, see the documentationDllEntryPoint; the same values are returned.
b.On DLL_PROCESS_ATTACH and DLL_THREAD_ATTACH (see"DllEntryPoint" in the Win32 API reference for more information onthese flags), call CRT_INIT(), first, before any C Run-timefunctions are called or any floating-point operations areperformed.
c.Call your own process/thread initialization/terminationcode.
d.On DLL_PROCESS_DETACH and DLL_THREAD_DETACH, call CRT_INIT()last, after all C Run-time functions have been called and allfloating- point operations are completed.
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: BOOL WINAPI DllEntryPoint(HINSTANCE hinstDLL, DWORD fdwReason,LPVOID lpReserved){if (fdwReason == DLL_PROCESS_ATTACH || fdwReason == DLL_THREAD_ATTACH)if (!_CRT_INIT(hinstDLL, fdwReason, lpReserved))return(FALSE);if (fdwReason == DLL_PROCESS_DETACH || fdwReason == DLL_THREAD_DETACH)if (!_CRT_INIT(hinstDLL, fdwReason, lpReserved))return(FALSE);return(TRUE);} 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 the .EXE creates a CRT file handle using _create() or_open(), this file handle may only be passed to _lseek(), _read(),_write(), _close(), etc. in the .EXE file. Do not pass this CRTfile handle to either DLL. Do not pass a CRT file handle obtainedfrom either DLL to the other DLL or to the .EXE.

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).

轉(zhuǎn)載于:https://www.cnblogs.com/ShaneZhang/p/3480934.html

與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的语言那点事,crt的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。