gcc检测静态库中的重复符号/函数 [英] gcc detect duplicate symbols/functions in static libraries

查看:980
本文介绍了gcc检测静态库中的重复符号/函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以让gcc检测静态库中的重复符号与主代码(或者另一个静态库?)

以下是情况:



main.c错误地包含了一个函数定义,例如与签名 uint foohash(const char *)



foo.c还包含一个函数定义, code> uint foohash(const char *)



foo.c和其他源文件被编译为静态实用程序库,主程序链接在内,例如:

  gcc -o main main.o util.o -L ./libs  - lfooutils 

因此,现在main.o和libs / libfooutils.a都包含一个foohash函数。推测链接器在main.o中找到了这个符号,并且不会在其他地方寻找它。



有没有办法让gcc检测到这种情况? / p>

解决方案

确实如Simon Richter所说, - whole-archive 选项可以有用。尝试将您的命令行更改为:

  gcc -o main main.o util.o -L ./libs -Wl, -  whole-archive -lfooutils -Wl, -  no-whole-archive 

,您将看到多重定义错误。


Is there any way we can get gcc to detect a duplicate symbol in static libraries vs the main code (Or another static library ?)

Here's the situation:

main.c erroneously contained a function definition, e.g. with the signature uint foohash(const char*)

foo.c also contains a function definition with the signature uint foohash(const char*)

foo.c and other source files are compiled to a static util library, which the main program links in, i.e. something like:

 gcc -o main main.o util.o -L ./libs -lfooutils

So, now main.o and libs/libfooutils.a both contain a foohash function. Presumably the linker found that symbol in main.o and doesn't bother looking for it elsewhere.

Is there any way we can get gcc to detect such a situation ?

解决方案

Indeed as Simon Richter stated, --whole-archive option can be useful. Try to change your command-line to:

 gcc -o main main.o util.o -L ./libs -Wl,--whole-archive -lfooutils -Wl,--no-whole-archive

and you'll see a multiple definition error.

这篇关于gcc检测静态库中的重复符号/函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆