gpt4 book ai didi

linux - 如何链接两个具有许多冲突功能的共享库

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:00:14 24 4
gpt4 key购买 nike

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

最佳答案

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

两个库链接的时候,他们应该控制了他们导出的符号,应该隐藏了“其他”库,但是他们没有……

will dlopen help?

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

您必须明确地从 A.soB.so 中查找您需要的符号,但这是您能做的最好的事情。

更新:

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

这最好通过在应导出的符号上使用 -fvisibility=hidden 标志和 __attribute__((visibility("default"))) 来完成。示例:

#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=hidden
nm -CD foo.so | grep Foo::
00000000000005bc T Foo::ExportedFunction()
00000000000005c6 T Foo::AnotherExportedFunction()

瞧:只有我们明确想要导出的东西才是。

关于linux - 如何链接两个具有许多冲突功能的共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14680481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com