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

I can load functions from dynamic library with linking this dl ,but I can not load it using 'dlsym' in the code without linking this dl

本文关键字:加载 dl 链接 代码 dlsym 动态 可以通过 函数 如果不      更新时间:2023-10-16

我在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++标识符通常被"篡改",以包含有关其命名空间和参数的信息(这在历史上有助于链接器区分重载函数)。您可能希望将函数设为extern "C"以防止损坏,或者找到其损坏的名称与dlsym一起使用(例如,在Linx上,在对象上使用nm,在源上使用gcc -S -o /dev/tty ... | grep some_func)。