+ -
当前位置:首页 → 问答吧 → 求助:如何让linux下ld自动检查.a包中的函数重名

求助:如何让linux下ld自动检查.a包中的函数重名

时间:2011-06-28

来源:互联网

有两个c文件,t.c和t2.c,分别定义了一个函数ttt();
按说编译之后再链接时,应该报错: multiple definition of `ttt'。但实际情况却是:

1) 如果我编译出两个obj(t.obj t2.obj),然后ld 这两个obj,的确会报错:
[root@localhost temp]# gcc -o t2.obj -c t2.c;gcc -o t.obj -c t.c
[root@localhost temp]# ld t.obj t2.obj
t2.obj: In function `ttt':
t2.c:(.text+0x0): multiple definition of `ttt'
t.obj:t.c:(.text+0xf): first defined here

但是,如果我把两个.obj ar成一个.a包,再针对.a包进行ld,则不会报错:
[root@localhost temp]# gcc -o t2.obj -c t2.c;gcc -o t.obj -c t.c;ar crus t.a t.obj t2.obj
[root@localhost temp]# gcc -o t2.obj -c t2.c;gcc -o t.obj -c t.c;ar crus t.a t.obj t2.obj;ld --start-group t.a --end-group -e main
[root@localhost temp]#  

现在我们代码工程很大,而且是分模块维护,互相之间通过.a进行版本共享,避免不了这个问题。


此问题原来被我发在 CSDN-CSDN社区-Linux/Unix社区-专题技术讨论区 ,  
http://topic.csdn.net/u/20110624/08/e42379c4-bffc-46bb-81e9-7bbc6f4cec3f.html
解决办法是使用nm报错:
nm -s software.a s5348.a drv_platform.a|awk 'toupper($2)==($2) {print $3}'|sort|uniq -d

但是nm写在makefile中不方便,而且此种做法有很多误报,工程大了改不过来。

希望这里的大大们能帮忙支招。多谢了!

作者: Solmyr_biti   发布时间: 2011-06-28

这个问题,我也没涉及到,等高人来看看吧.帮你顶了.

作者: abao623660072   发布时间: 2011-06-28