How to support dynamic plugins when statically linking in standard libraries?

大城市里の小女人 提交于 2021-02-10 05:40:50

问题


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.

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?

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.

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: *;
};

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.


回答1:


Yes, this solution would work but

  • it introduces certain overhead due to code duplication
  • it may break if you try to pass STL objects between plugin/app due to ABI changes in recent libstdc++

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.



来源:https://stackoverflow.com/questions/47841812/how-to-support-dynamic-plugins-when-statically-linking-in-standard-libraries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!