在标准库中静态链接时如何支持动态插件? [英] How to support dynamic plugins when statically linking in standard libraries?
问题描述
假设使用 g ++
构建应用程序 myapp.exe
,并且使用标志 -static-libstdc ++
以便将其安装在没有 libstdc ++.so
的环境. myapp.exe
还为某些功能 plugf
添加了插件支持,该功能可以通过 dlopen
从分片库动态加载.
Suppose an application myapp.exe
is built using g++
and it uses the flag -static-libstdc++
so that it can be installed in enviroments without libstdc++.so
. myapp.exe
also adds plugin support for some function plugf
that can be dynamically loading via dlopen
from a shard library.
如果 libplug.so
是一个也链接到 libstdc ++
的插件库,那么如何才能与 myapp一起使用.exe
?
If libplug.so
is such a plugin library that also links to libstdc++
, how can it do so in a way to be able to work with myapp.exe
?
如果动态链接了 libstdc ++
,这很简单,因为 myapp.exe
和 libplug.so
都可以使用相同的动态加载标准库,但我尚不清楚如何使用静态链接的标准库来做到这一点.
This is straightforward if libstdc++
is linked in dynamically since both myapp.exe
and libplug.so
can use the same dynamically loaded standard library, but it's not clear to me how best to do this with statically linked standard libraries.
我正在考虑的一种方法是让 libplug.so
也使用标志 -static-libstdc ++
,然后使用
An approach I'm considering is to have libplug.so
also use the flag -static-libstdc++
and then use the version script
{
global: plugf;
local: *;
};
以确保使用其标准库的版本,但这意味着将有两个 libstdc ++
副本复制到内存中.我知道C ++标准不会祝福这种方法,因为它会违反ORD,但是 libstdc ++
是否以任何方式支持这种方法?手册多项ABI测试部分>部分确实引用了听起来相似的情况.
to ensure that its version of the standard library is used, but that would mean there would be two copies of libstdc++
loaded into memory. I know this approach wouldn't be blessed by the C++ standard since it would have ORD violations, but is it something that libstdc++
supports in any way? The Multiple ABI testing
section of its manual section does reference a scenario that sounds similar.
推荐答案
是的,该解决方案可以工作,但
Yes, this solution would work but
- 由于代码重复,它会带来一定的开销
- 由于最近libstdc ++中的ABI更改,如果您尝试在插件/应用之间传递STL对象,它可能会中断
删除 std :: allocator
在不同的C ++运行时中分配的对象应该在Linux上可行,尽管我在libstdc ++文档中找不到与此相关的明确声明.正如Mikhail在评论中指出的那样,它确实在Windows上失败.
Deleting objects allocated by std::allocator
in different C++ runtime should work on Linux although I could not find explicit statement about this in libstdc++ docs. It does fail on Windows as Mikhail pointed out in comments.
这篇关于在标准库中静态链接时如何支持动态插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!