c++ - 我可以通过链接此 dl 从动态库加载函数,但如果不链接此 dl,我无法在代码中使用 'dlsym' 加载它

标签 c++ c linux compiler-construction linker

我在fedora 116上使用gcc/g++,我的想法是:

c program -> load c++ dynamic library A -> load c++ dynamic library B

c++动态库B是第三方提供的,我无法修改。

当编译 C++ 动态库 A 并链接 C++ 动态库 B 时,A 可以在 B 中找到符号。但是当我使用“dlsym”在 A 代码(不链接)中加载 B 函数时,A 告诉我

/path/to/B.so: undefined symbol: some_func

=============================

使用 nm -DC

0000000000014a80 T BinarySearch(int, int*, int)
0000000000007210 T CheckLicense()
0000000000009370 T GetEnd(stCha*, int&, int)
000000000000a970 T IC_Exit()
000000000000a740 T IC_Init(char const*)

错误报告:

/path/to/some.so undefined symbol: IC_Init

库A中的代码:

IC_API bool (* IC_Init)(const char *);
IC_Init = (IC_API bool (*)(const char *)) dlsym(dl_ic, "IC_Init");
if(IC_Init) {
    printf("function loaded");
}

在库A中,它可以使用dlopen加载库B:

void *dl_ic = dlopen(ic_lib_path, RTLD_LAZY);

最佳答案

您是否考虑过名称修改? C++ 标识符通常被“破坏”以合并有关其 namespace 和参数的信息(这在历史上帮助链接器区分重载函数)。您可能希望创建函数 extern "C" 以防止损坏,或者找到它的损坏名称以与 dlsym 一起使用(例如,在 Linx 上使用 nm 在对象上,或在源代码上 gcc -S -o/dev/tty ... | grep some_func

关于c++ - 我可以通过链接此 dl 从动态库加载函数,但如果不链接此 dl,我无法在代码中使用 'dlsym' 加载它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526688/

相关文章:

c - 在 Linux C 中播放音频文件的库

c++ - 为什么 iostream 对象不重载 operator bool?

java - Java 中 C 的 "_getch()"的等价函数?

c++ - 使用字符串文字的编码约定

c++ - 并发 : Are Python extensions written in C/C++ affected by the Global Interpreter Lock?

c++ - 为什么节点*左,*右而不是节点*左,右?

linux - 如何将文件名(以及子目录)中的双空格替换为一个空格(CloudLinux Server 版本 6.10)

linux - 如何找到有模式的子串?

android - 带有 STLport_shared 的 NDK 用于独立二进制文件?

c++ - 如何对 std :list when you need member data? 进行排序