質問

say I have a parent and a child, the child calls a function "hello" in the child with dlopen. Can the child then call a function "world" in the parent? I keep getting symbol lookup error: ./child.so: undefined symbol: world

here are the files. 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");
}

and child.c

#include <stdio.h>
void hello () {
 printf ("hello");
 world();
}
役に立ちましたか?

解決

Yes, it a dlopen-ed module can call functions from the calling program, provided that the calling program has been linked with the -rdynamic option.

BTW, most plugins need that feature: a firefox plugin obviously wants to call firefox functions.

Read also about visibility function __attribute__ ... Read also Drepper's How to Write Shared Libraries long paper and dlopen(3) man page.

他のヒント

I found the answer on google

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

gcc -rdynamic hello.c -ldl

Surprisingly it can. I've personally seen it happen.

You may need to link to the executable with -rdynamic

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top