再次回想C库函数链接

最近又开始瞎折腾起来了,有点弄不清对C库的链接。

C Run-Time Libraries (CRT)

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

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

/MD

_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

 

Note Note

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值