首页 > 其他 > 详细

链接时库的顺序

时间:2015-10-12 20:40:37      阅读:198      评论:0      收藏:0      [点我收藏+]

demo0.c

int fun1()
{
return 0;
}

 

demo1.c

int fun1();

int fun2()
{
fun1();
return 0;
}

 

main.c

#include <stdio.h>
int fun2();
void main()
{
fun2();
printf("%d\n","yeah");
}

编译结果如下:

[root@localhost tmp]# gcc demo0.c -c -o demo0.o
[root@localhost tmp]# ar rcs libdemo0.a demo0.o
[root@localhost tmp]# gcc demo1.c -c -o demo1.o
[root@localhost tmp]# ar rsc libdemo1.a demo1.o
[root@localhost tmp]# ll
total 6
-rwxrwxrwx. 1 root root 26 Oct 12 18:32 demo0.c
-rwxrwxrwx. 1 root root 1232 Oct 12 18:35 demo0.o
-rwxrwxrwx. 1 root root 48 Oct 12 18:33 demo1.c
-rwxrwxrwx. 1 root root 12 Oct 12 18:34 demo1.h
-rwxrwxrwx. 1 root root 1368 Oct 12 18:36 demo1.o
-rwxrwxrwx. 1 root root 1374 Oct 12 18:36 libdemo0.a
-rwxrwxrwx. 1 root root 1510 Oct 12 18:36 libdemo1.a
-rwxrwxrwx. 1 root root 48 Oct 12 18:34 main.c

[root@localhost tmp]# gcc main.c -L. -ldemo0 -ldemo1
./libdemo1.a(demo1.o): In function `fun2‘:
demo1.c:(.text+0xa): undefined reference to `fun1‘
collect2: ld returned 1 exit status
[root@localhost tmp]# gcc main.c -L. -ldemo1 -ldemo0
[root@localhost tmp]# ./a.out
4195852

说明在链接库时函数是向后查找的,具体的排序应该是调用库,被调用库,被被调用库。

具体的感受大家自己体会。

 

那么为什么会是这个样子呢?

我觉得是和gcc编译器的实现有关。

链接时库的顺序

原文:http://www.cnblogs.com/leo0000/p/4872552.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!