如何与两个具有许多冲突函数的共享库链接 [英] how to link with two shared libraries with many conflicting functions

查看:143
本文介绍了如何与两个具有许多冲突函数的共享库链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在Linux上与两个第三方共享库(A.so和B.so)进行链接。问题是两者都与另一个库静态链接,因此从A.so和B.so获得约400个函数具有相同的名称。
当我编译并链接-lA -lB或-lB -lA时,根据顺序,函数从A或B中分别被拾取,这是由于函数中断导致的问题,代码无法运行。我想知道是否有一种方法可以将函数名绑定到它们的库,这样两个库可以被链接和使用?因为那些重叠的函数名在A和B内部被内部调用,所以我不能使用像objcopy这样的东西,等dlopen的帮助吗?

解决方案


我想知道是否有方法将函数名绑定到它们的库,以便这两个库可以链接和使用?

当两个图书馆联系在一起时,他们应该控制他们出口的符号,并且应该隐藏其他图书馆,但是他们没有......


会提供帮助吗?

是:如果您 dlopen(A.so,RTLD_LOCAL); dlopen(B.so,RTLD_LOCAL); ,那么这两个库都不会添加到全局范围,他们不会看到对方。



您必须从 A显式查找您需要的符号。所以 B.so ,但这是您能做的最好的选择。 $ b

<强>更新


有没有一种快速的方法可以链接到一个静态库,而不需要在构建A.so时从该其他库中导出符号。 p>

最好使用 -fvisibility = hidden 标志和 __属性__((可见性(默认)))应该被导出的符号。例子:

  #define EXPORTED __attribute __((visibility(default)))

struct Foo {
void EXPORTED ExportedFunction();
void EXPORTED AnotherExportedFunction();
void InternalFunction();
};

void Foo :: ExportedFunction(){}
void Foo :: AnotherExportedFunction(){}
void Foo :: InternalFunction(){}


gcc -shared -fPIC -o foo.so foo.cc
nm -CD foo.so | grep Foo ::
00000000000005fc T Foo :: ExportedFunction()
0000000000000610 T Foo :: InternalFunction()
0000000000000606 T Foo :: AnotherExportedFunction()

如果没有显式的导出控制,所有东西都会被导出(包括 InternalFunction 我们不想要)。

  gcc -shared -fPIC -o foo.so foo.cc -fvisibility =隐藏
nm -CD foo。所以| grep Foo ::
00000000000005bc T Foo :: ExportedFunction()
00000000000005c6 T Foo :: AnotherExportedFunction()

Voilà:只有我们明确要出口的东西是。


I'm currently linking with two third party shared libraries (A.so and B.so) on linux. the problem is that both of the two so's linked statically with another library, as a result there are about 400 functions from A.so and B.so having the same names. When I compile and link with -lA -lB, or -lB -lA, depending on the order the functions are picked up from A or B separately as a result of function interposition which caused problem and the code cannot run. I'm wondering if there's a way to bind function names to their libraries so both libraries can be linked and used? because those overlapping function names are called internally within A and B, so I can't use things like objcopy, etc. will dlopen help?

解决方案

I'm wondering if there's a way to bind function names to their libraries so both libraries can be linked and used?

When the two libraries were linked, they should have controlled the symbols they export, and should have hidden the "other" library, but they didn't ...

will dlopen help?

Yes: if you dlopen("A.so", RTLD_LOCAL); and dlopen("B.so", RTLD_LOCAL);, then neither library will be added to the global scope, and they will not "see" each other.

You'll have to explicitly lookup symbols you need from A.so and B.so, but that's the best you could do.

Update:

is there a quick way to link to a static library without exporting symbols from that "other" library while building A.so

This is best done by using -fvisibility=hidden flag and __attribute__((visibility("default"))) on symbols that should be exported. Example:

#define EXPORTED __attribute__((visibility("default")))

struct Foo {
  void EXPORTED ExportedFunction();
  void EXPORTED AnotherExportedFunction();
  void InternalFunction();
};

void Foo::ExportedFunction() { }
void Foo::AnotherExportedFunction() { }
void Foo::InternalFunction() { }


gcc -shared -fPIC -o foo.so foo.cc
nm -CD foo.so  | grep Foo::
00000000000005fc T Foo::ExportedFunction()
0000000000000610 T Foo::InternalFunction()
0000000000000606 T Foo::AnotherExportedFunction()

Without explicit export control, everything gets exported (including InternalFunction we don't want).

gcc -shared -fPIC -o foo.so foo.cc -fvisibility=hidden
nm -CD foo.so  | grep Foo::
00000000000005bc T Foo::ExportedFunction()
00000000000005c6 T Foo::AnotherExportedFunction()

Voilà: only things we explicitly wanted to export are.

这篇关于如何与两个具有许多冲突函数的共享库链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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