dlopened 模块可以调用调用者中的函数吗?

标签 c dlopen

假设我有一个 parent 和一个 child , child 用 dlopen 调用 child 中的函数“hello”。那么子级可以调用父级中的函数“world”吗?我不断收到符号查找错误:./child.so: undefined symbol :世界

这是文件。父级.c

#include <dlfcn.h>
typedef void (*fptr)();
#include <stdio.h>

int main () {
 void*handle=dlopen("./child.so",RTLD_LAZY);
 fptr f=dlsym(handle,"hello");
 f();
 return 0;
}

void world() {
 printf ("world");
}

和child.c

#include <stdio.h>
void hello () {
 printf ("hello");
 world();
}

最佳答案

是的,dlopen 模块可以从调用程序调用函数,前提是调用程序已与 -rdynamic 选项链接。

顺便说一句,大多数插件都需要该功能:firefox 插件显然想要调用 firefox 函数。

另请阅读 visibility功能__attribute__ ...另请阅读 Drepper's How to Write Shared Libraries长纸和dlopen(3)手册页。

关于dlopened 模块可以调用调用者中的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20765526/

相关文章:

c - 在数组中查找素数和复合元素。按升序打印质数并按降序合成

shared-libraries - 共享库以什么顺序初始化和完成?

android - java.lang.UnsatisfiedLinkError : dlopen failed: cannot locate symbol "cmsg_nxthdr" referenced by "libpcap.so" 错误

c - 如何在 Linux C 中从库和地址中获取函数名

c - 无论如何指示 dlopen 不要在 C (Linux) 中加载不兼容的二进制文件

c - (C) 当以文本模式而不是二进制模式写入文件时,哪些字符会受到影响?

c - 如何在同一行初始化包含 const char* 的结构和 const char* 本身?

c - C中字符串的哈希函数

c - 位板到位板(三元位板)转换

c - gcc __attribute__((constructor)) 到底什么时候运行?