一个倾斜的模块可以调用调用者中的函数吗?

时间:2013-12-24 18:49:48

标签: c dlopen

说我有一个父母和一个孩子,孩子用dlopen在孩子中调用一个函数“hello”。然后孩子可以在父母中调用“世界”功能吗?我不断收到符号查找错误:./ child.so:undefined symbol:world

这是文件。 parent.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();
}

3 个答案:

答案 0 :(得分:1)

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

BTW,大多数插件都需要这个功能:firefox插件显然想要调用firefox函数。

另请阅读visibility函数__attribute__ ...另请阅读Drepper's How to Write Shared Libraries长篇论文和dlopen(3)手册页。

答案 1 :(得分:1)

我在谷歌找到了答案

http://www.justskins.com/forums/dlopen-calling-functions-other-104185.html

gcc -rdynamic hello.c -ldl

答案 2 :(得分:0)

令人惊讶的是它可以。我亲眼看到它发生了。

您可能需要使用-rdynamic

链接到可执行文件