linux下gcc默认的链接可以是哪些,linux – 关于GCC链接器搜索顺序的一些问题

我对

gcc链接顺序有一些疑问. GCC man表示链接器从左到右搜索符号,默认情况下不重复搜索.这是我的测试:

main.c中

#include

#include

int main()

{

printf("HELLO WROLD\n");

return 0;

}

printf.c

#include

#include

int printf(const char *fmt,...)

{

write(1,"AAA\n",4);

}

[root@lenovo testcode]# gcc -c -fno-builtin-printf *.c

[root@lenovo testcode]# gcc -o test main.o printf.o

[root@lenovo testcode]# ./test

AAA

[root@lenovo testcode]# gcc -o test printf.o main.o

[root@lenovo testcode]# ./test

AAA

[root@lenovo testcode]# ar rcs libprintf.a printf.o

[root@lenovo testcode]# gcc -o test libprintf.a main.o

[root@lenovo testcode]# ./test

HELLO WROLD

[root@lenovo testcode]# gcc -o test main.o libprintf.a

[root@lenovo testcode]# ./test

AAA

[root@lenovo testcode]# gcc -shared -o libprintf.so printf.o

[root@lenovo testcode]# gcc -o test libprintf.so main.o

[root@lenovo testcode]# export LD_LIBRARY_PATH=.

[root@lenovo testcode]# ./test

AAA

[root@lenovo testcode]# gcc -o test main.o libprintf.so

[root@lenovo testcode]# ./test

AAA

从结果中,我们可以看到.o和.o,.o和.so的顺序没有区别,只有.o和.a的顺序才有效.但这与gcc手册页不一致.所以为什么?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值