问题描述
下面是我的代码:
//a.c I don't use header files as this is just for demo purpose.
extern void function_b(int num);
void function_a(int num)
{
function_b(num)
}
//b.c
void function_b(int num)
{
...
}
//dll.c
#include <dlfcn.h>
int main()
{
void *handle_a;
void *handle_b;
void (*pfunc_a)(int);
...
handle_a = dlopen("./a.so",RTLD_LAZY);
...
pfunc_a = dlsym(handle_a,"function_a");
...
handle_b = dlopen("./b.so",RTLD_GLOBAL);
...
pfunc_a(2020);
...
return 0;
}
我们可以看到dll.c
尝试在运行时加载共享库,并且模块a
在function_b
上有一个引用,而模块b
的定义是{{1 }}。假设我们已经创建了共享库function_b
和a.so
,因此这些共享库在程序运行之前就已存在于磁盘上。
但是当我运行程序时,它会引发符号查找错误:
./ a.so:未定义符号:function_b
但对于这一行代码b.so
以下是我对handle_a = dlopen("./a.so",RTLD_LAZY);
标志的理解:
由于我在这里使用RTLD_LAZY
,因此运行时链接程序不会尝试解析符号RTLD_LAZY
,并且有机会在调用function_b
之前先调用dlopen("b.so",RTLD_GLOBAL)
。这样,动态链接程序将使用b.so中的function_a
的定义来修改a.so
中的引用。
如果我的理解是正确的,那么为什么动态链接程序在这种情况下仍无法解析function_b
?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)