dynamic-linking - 动态加载: Undefined Symbol In Shared Static Library

标签 dynamic-linking dynamic-loading

我有一个加载 .so 插件的可执行文件。

可执行文件与-rdynamic链接,以便可以发生符号回调。

我有一个包含在可执行文件中的静态库。 .a

中有一个名为 BLAH_hello() 的函数

可执行文件中未使用静态库。即可执行代码中没有调用 BLAH_hello()

但是,.so 确实调用了 BLAH_hello()

当我 dlopen() .so 时,它向 BLAH_hello() 提示 undefined symbol

如果我在可执行代码中包含对 BLAH_hello() 的虚拟调用,例如 BLAH_hello(NULL);。该符号包含在可执行文件中,当加载 .so 时,它会找到该符号。

我确信我也可以将 .so 链接到 .a,但多个动态加载的 .so 使用 BLAH_hello 调用,因此将其放在可执行文件中是有意义的。如果我将库链接到每个 .so 中,我还担心符号冲突。

所以我想知道的是,如何将 .a 的符号放入可执行文件中,即使它们实际上没有在可执行文件中使用

最佳答案

When I dlopen() the .so it complains about an undefined symbol to BLAH_hello()

If I include a dummy call to BLAH_hello() in the executable code, like BLAH_hello(NULL);. The symbol is included in the executable and when the .so is loaded it finds the symbol.

这是完全正常且符合预期的。只是how linkers work with archive libraries .

如果必须将 BLAH_hello 包含到主可执行文件中,请将 -Wl,-u,BLAH_hello 添加到可执行链接行。

关于dynamic-linking - 动态加载: Undefined Symbol In Shared Static Library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6135603/

相关文章:

c++ - 从 C++ 类制作 Visual C++ DLL

c++ - 是否有可能不让我的可执行文件中的符号可用于动态打开的库?

c++ - 动态加载库中的 thread_local 静态变量——它们是什么时候创建的?

c++ - 使用 dlopen 动态加载共享库

c - 为什么在 C 中对动态链接的符号执行指针运算时会得到错误的结果?

c++ - libboost_*.so : file not recognized: File truncated When dynamically linking using libtool and automake to generate makefile

macos - 为什么 OS X 上的共享库要与绝对路径链接?

mongodb - mongo.exe - 找不到入口点

c++ - dlopen'ing 时是否运行静态初始化(和/或其他)代码?

c++ - 动态加载和动态绑定(bind)的区别