程序使用两个冲突的共享库 [英] Program use two conflicting shared libraries

查看:137
本文介绍了程序使用两个冲突的共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个共享库 A.so 。其中定义了一个函数 foo()。此 foo()函数取决于共享库 libnl-1.so 。关系如下:

  A.so 
{
foo()=> libnl-1
}

我有一个程序 app 。它调用两个函数 foo() bar() bar()需要另一个libnl版本,libnl-3。关系如下:

  app {
foo()
bar()=> libnl-3
}

我使用 cc -o编译了应用程序应用-lnl-3 -lA 。但是我发现我的应用程序总是崩溃。似乎 foo()正在调用libnl-3而不是libnl-1(我不知道如何验证这一点)。谁能帮我吗?如果我想这样做,该怎么办?更改链接顺序?

解决方案


如果我想这样做,该怎么办?


在UNIX上(与Windows DLL不同),共享库不是自包含单元,并且不能单独运行。 UNIX共享库的设计是尽可能模拟存档库。结果之一是(默认情况下)第一个定义的函数获胜。在您的情况下, libnl-3 libnl-1 可能定义了相同的函数,您将获得定义


从哪个库中是 first (一次调用是错误的)。链接顺序?


这将更改第一个库,并且 still 是错误的。



那么,您应该做什么



最好的选择是不是链接同一库的不兼容版本。选择 libnl-1 libnl-3 之一并坚持使用。 p>

如果不能,则通过链接 A.so 可能达到预期的效果c>与 -Bymbolic ,或通过使 bar 使用 dlopen( libnl-3。因此,RTLD_LOCAL | RTLD_LAZY)来查找所需的 libnl-3 函数,而不是直接使用它。


I have a shared library A.so. There is a function foo() defined in it. This foo() function depends on a shared library libnl-1.so. The relationship is below:

A.so 
    {
      foo() => libnl-1
    } 

I have a program app. It calls two functions, foo() and bar(). bar() needs another version of libnl, libnl-3. The relationship is below:

app {
      foo()
      bar() => libnl-3
    }

I compiled app using cc -o app -lnl-3 -lA. But I found my app always crashes. It seems that foo() is calling into libnl-3 instead of libnl-1 (I have no idea how to verify this). Can anyone help me out? If I want to do this, what should I do? Change the linking order?

解决方案

If I want to do this, what should I do?

On UNIX (unlike a windows DLL), a shared library is not a self-contained unit, and does not function in isolation. The design of UNIX shared libraries is to emulate archive libraries as much as possible. One of the consequences is that (by default) the first defined function "wins". In your case, libnl-3 and libnl-1 likely define the same functions, and you'll get the definition from whichever library is first (which will be wrong for one call, or the other).

Change the linking order?

That will change the first library, and will still be wrong.

So, what should you do?

The best option is not to link incompatible versions of the same library. Pick one of libnl-1 or libnl-3 and stick with it.

If you can't, you may be able to achieve desired result by linking A.so with -Bsymbolic, or by making bar use dlopen("libnl-3.so", RTLD_LOCAL|RTLD_LAZY) to lookup needed libnl-3 function instead of using it directly.

这篇关于程序使用两个冲突的共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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