Why does an std::any_cast of a passed std::any inside a dlopen'd function raise an error

纵然是瞬间 提交于 2019-12-06 03:42:35

libstdc++'s any relies on the address of the same template instantiation being the same inside a program, and that means you need to take precautions if you are using dlopen:

The compiler has to emit [objects with vague linkage, like template instantiations] in any translation unit that requires their presence, and then rely on the linking and loading process to make sure that only one of them is active in the final executable. With static linking all of these symbols are resolved at link time, but with dynamic linking, further resolution occurs at load time. You have to ensure that objects within a shared library are resolved against objects in the executable and other shared libraries.

  • For a program which is linked against a shared library, no additional precautions are needed.
  • You cannot create a shared library with the -Bsymbolic option, as that prevents the resolution described above.
  • If you use dlopen to explicitly load code from a shared library, you must do several things. First, export global symbols from the executable by linking it with the -E flag (you will have to specify this as -Wl,-E if you are invoking the linker in the usual manner from the compiler driver, g++). You must also make the external symbols in the loaded library available for subsequent libraries by providing the RTLD_GLOBAL flag to dlopen. The symbol resolution can be immediate or lazy.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!