最近又开始瞎折腾起来了,有点弄不清对C库的链接。
The following libraries contain the C run-time library functions.
C run-time library (without iostream or standard C++ library) | Associated DLL | Characteristics | Option | Preprocessor directives |
---|---|---|---|---|
libcmt.lib | None, static link. | Multithreaded, static link | _MT | |
msvcrt.lib | msvcr80.dll | Multithreaded, dynamic link (import library for MSVCR80.DLL). Be aware that if you use the Standard C++ Library, your program will need MSVCP80.DLL to run. | _MT, _DLL | |
libcmtd.lib | None, static link | Multithreaded, static link (debug) | /MTd | _DEBUG, _MT |
msvcrtd.lib | msvcr80d.dll | Multithreaded, dynamic link (import library for MSVCR80D.DLL) (debug). | /MDd | _DEBUG, _MT, _DLL |
msvcmrt.lib | msvcm80.dll | C Runtime import library. Used for mixed managed/native code. | /clr |
|
msvcurt.lib | msvcm80.dll | C Runtime import library compiled as 100% pure MSIL code. All code complies with the ECMA URT spec for MSIL. | /clr:pure |
|
![]() |
---|
The single-threaded CRT (libc.lib, libcd.lib) (formerly the /ML or /MLd options) is no longer available. Instead, use the multithreaded CRT. See Multithreaded Libraries Performance . |
If you link your program from the command line without a compiler option that specifies a C run-time library, the linker will use LIBCMT.LIB. This is different from previous versions of Visual C++ which used LIBC.LIB, the single-threaded library, instead.
上面一段摘自最新的MSDN ,要链接的C库是由Option指定,但是这是编译器选项,链接器并不知道。其实编译器会根据Option吧对应的库写入obj文件,如-defaultlib:LIBCD -defaultlib:OLDNAMES(用任意编辑器打开,如果用IDA打开obj文件就更明白了)。#pragma comment(lib/linker……)等其实也都写入了obj文件的那个位置,链接器会读取那里的数据作为参数传入。终于清楚些了,O(∩_∩)O哈哈~
忽然又想起一个问题,很多C库函数都要调用win32的API,如fopen()、malloc()等,由于这些API包含在C运行时库(CRT)中,所以在链接时并不用手动指定它们的lib库,那么链接器又是如何链接CRT中的win32 API的呢?那一个VC6下最简单的一个C静态库libc.lib(单线程)为例,lib文件其实是由很多obj文件组成的,可以用libx把它解开(网上搜),也可以直接用IDA查看,在crt0init.obj中可以看到defaultlib:kernel32.lib,对应的源文件/VC98/CRT/SRC/CRT0INIT.C是这样的:
现在一切都明朗了……
附上我以前写的两篇文章:
http://blog.csdn.net/better0332/archive/2008/12/19/3557654.aspx
http://blog.csdn.net/better0332/archive/2008/12/18/3552856.aspx