如何在 g++ 中链接同一库的不同版本? [英] How do I link different versions of the same library in g++?
问题描述
我试图弄清楚如何在 SLES10 机器上加载两个不同版本的 libstdc++.so.我的客户端有一个进程 Foo,它是用 GCC 4.1.2 构建的,因此使用 6.0.8 版本的 libstdc++.so.我们还在构建名为 libBar.so 的共享库.这个库将在运行时由 Foo 动态加载.libBar.so 使用 GCC 4.3.6 和 libstdc++ 版本 6.0.10 编译.
I'm trying to figure out how to load two different versions of libstdc++.so on a SLES10 machine. My client has a process Foo, which is built with GCC 4.1.2, and thus uses the 6.0.8 version of libstdc++.so. We are also building shared library called libBar.so. This library will be dynamically loaded by Foo at runtime. libBar.so is compiled using GCC 4.3.6, and libstdc++ version 6.0.10.
目前,当我尝试让 Foo 加载 libBar.so 时,出现以下错误.
Currently, when I try to have Foo load libBar.so, I get the following error.
错误:无法加载共享对象'/usr/lib64/libBar.so':/usr/lib64/libstdc++.so.6:未找到版本GLIBCXX_3.4.9"(/usr/lib64/libBar.so 需要)
error: unable to load shared object '/usr/lib64/libBar.so': /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by /usr/lib64/libBar.so)
目前,我可以让它工作的唯一方法是更改我的库加载顺序(通过 ld.so.conf),以便 Foo 和 libbar.so 都加载相同的 (6.0.10) libstdc++.所以.但是,这不是一个可行的解决方案,因为它需要我修改客户端的系统.
At the moment, the only way that I can get this to work is to change my library load order (via ld.so.conf) so that Foo and libbar.so both load the same (6.0.10) libstdc++.so. However, this isn't a vialbe solution, since it requires that I modify the client's system.
我想做的是让 Foo 加载它的 libstdc++.so 版本和 libBar.so 链接到它自己的 libstdc++.so 版本,但我不知道如何编写我的 Makefile 来实现这一点.这是我目前所拥有的,对于 Makefile.am 中的 LIBADD 行...
What I'd like to do is have Foo load it's verions of libstdc++.so and libBar.so link to it's own version of libstdc++.so, but I can't figure out how to write my Makefile to make that happen. Here's what I have so far, for my LIBADD line in Makefile.am...
libBar_la_LIBADD = ../../vendor/SLES10/lib/libstdc++.so.6.0.10
libBar_la_LIBADD = ../../vendor/SLES10/lib/libstdc++.so.6.0.10
我假设想要那个特定版本的 libstdc++.so.但是,当我对完全编译和链接的 libBar.so 运行 ldd 时,这是我看到的行...
Which I would assume would like to that SPECIFIC version of libstdc++.so. However, when I run ldd against the fully compiled and linked libBar.so, this is the line I see...
libstdc++.so.6 =>/usr/lib64/libstdc++.so.6 (0x00002aaaaeac5000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00002aaaaeac5000)
为什么它不专门链接到 libstdc++.so.6.0.10?我应该怎么做?
Why isn't it linking specifically to libstdc++.so.6.0.10? What should I be doing instead?
推荐答案
我在使用过时版本的 libstdc++
的第 3 方库中遇到了类似问题.
I had a similar issue with a 3rd-party library that used an obsolete version of libstdc++
.
我通过将第 3 方库与旧版本的 libstdc++
进行静态预链接解决了这个问题.最终结果是另一个没有未解析的 libstdc++
符号的共享库.命令行是这样的:
I solved it by pre-linking the 3rd-party library statically with that old version of libstdc++
. The end result was another shared library that did not have unresolved libstdc++
symbols. The command line was something like this:
ld --relocatable -o lib3rd-party-prelinked.so lib3rd-party.so /usr/lib64/libstdc++.a.6
然后我使用 lib3rd-party-prelinked.so
而不是 lib3rd-party.so
.(在 man ld
中查找 --relocatable
).
And then I used lib3rd-party-prelinked.so
instead of lib3rd-party.so
. (Lookup --relocatable
in man ld
).
在我的情况下这是可能的,因为第 3 方库暴露了 C API,其接口中没有使用 C++ 标准库组件.
It was possible in my case because the 3rd-party library exposed a C API, no C++ standard library components were used in its interface.
如果您的第 3 方库在其接口中公开了 C++ 标准库类,其 ABI 在这些 libstdc++
版本之间是不同的,那将无法正常工作.例如.您的应用程序将带有新 ABI 的 std::list<>
传递给第三方库,该库需要带有旧 ABI 版本的 std::list<>
.即使它链接,也会在运行时导致未定义的行为.
If your 3rd-party library exposes C++ standard library classes in its interface whose ABIs are different between these libstdc++
versions, that is not going to work. E.g. your application passes a std::list<>
with the new ABI to the 3rd-party library that expects a std::list<>
with the old ABI version. Even if it links, that will cause undefined behaviour at run-time.
这篇关于如何在 g++ 中链接同一库的不同版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!