如何处理使用binutils链接器的静态库之间的递归依赖关系? [英] How to deal with recursive dependencies between static libraries using the binutils linker?

查看:179
本文介绍了如何处理使用binutils链接器的静态库之间的递归依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将现有的系统从Windows移植到Linux。该构建包含多个静态库。我遇到了一个链接错误,其中一个符号(在libA中定义)无法在libB的对象中找到。链接器行看起来像

g ++ test_obj.o -lA -lB -o test



当然问题因为在链接器发现它需要libA中的符号时,它已经传递了它,并且不会重新扫描,所以即使该符号存在,也只是出错。



我最初的想法当然是简单地将链接交换为(-lB -lA),以便随后扫描libA,并从libB中找到libA中缺少的任何符号。但后来我发现实际上libA和libB之间存在递归依赖关系!我假设Visual C ++链接器以某种方式处理它(它是否默认重新扫描?)。

处理这个问题的方法我已经考虑过了:

>


  • 使用共享对象。不幸的是,从需要PIC compliation(这是对性能敏感的代码并且失去%ebx来保持GOT真的会受到伤害)的角度来看,这是不可取的,并且不需要共享对象。

  • 构建一个包含所有对象的大型对象,避免出现问题。重新构造代码以避免递归依赖性(显然正确的事情,但我试图做这个端口与最小的变化)。




你有其他想法来处理这个?有什么方法可以说服binutils链接程序执行它在缺少符号时已经查看过的库的重新扫描吗? 解决方案

只要做到这一点:

  g ++ test_obj.o -lA -lB -lA -o test 

当链接器在命令行中读取第一个libA时,它会放弃其中没有人依赖的对象/符号,例如libB需要的所有符号,但不是test_obj.o。因此,您只需再次读取libA,它就会选取这些符号。


I'm porting an existing system from Windows to Linux. The build is structured with multiple static libraries. I ran into a linking error where a symbol (defined in libA) could not be found in an object from libB. The linker line looked like

g++ test_obj.o -lA -lB -o test

The problem of course being that by the time the linker finds it needs the symbol from libA, it has already passed it by, and does not rescan, so it simply errors out even though the symbol is there for the taking.

My initial idea was of course to simply swap the link (to -lB -lA) so that libA is scanned afterwards, and any symbols missing from libB that are in libA are picked up. But then I find there is actually a recursive dependency between libA and libB! I'm assuming the Visual C++ linker handles this in some way (does it rescan by default?).

Ways of dealing with this I've considered:

  • Use shared objects. Unfortunately this is undesirable from the perspective of requiring PIC compliation (this is performance sensitive code and losing %ebx to hold the GOT would really hurt), and shared objects aren't needed.

  • Build one mega ar of all of the objects, avoiding the problem.

  • Restructure the code to avoid the recursive dependency (which is obviously the Right Thing to do, but I'm trying to do this port with minimal changes).

Do you have other ideas to deal with this? Is there some way I can convince the binutils linker to perform rescans of libraries it has already looked at when it is missing a symbol?

解决方案

Just do this:

g++ test_obj.o -lA -lB -lA       -o test

When the linker reads the first libA on the command line, it'll discard the object/symbols in it that noone has depended on yet, e.g. all the symbols libB needs but not test_obj.o. So you just make it read libA again, and it'll pick up the those symbols as well.

这篇关于如何处理使用binutils链接器的静态库之间的递归依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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