我试图使用dlopen和dlsym来获得函数指针,但是我无法使它正常工作。它在尝试执行dlsym调用时失败。以下是我的密码。
有什么帮助吗?
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
int test() {
printf("%s", "test()");
return 123;
}
int main() {
char * functionname = "test";
void* handle = dlopen(NULL,RTLD_LAZY|RTLD_GLOBAL);
if (!handle) {
fprintf(stderr, "Couldn't open handle: %s\n",
dlerror());
exit(1);
}
int (*fun)() = (int (*)())dlsym(handle, functionname);
if (fun == NULL) {
fprintf(stderr, "Couldn't find function: %s\n",functionname);
exit(1);
}
int a = fun();
printf("result: %d \n", a);
}发布于 2014-03-21 14:59:46
您可能需要指定链接器才能将符号导出为动态符号。对于gcc,你必须使用-rdynamic。
您可以使用objdump -T检查导出的动态符号。
https://stackoverflow.com/questions/22561108
复制相似问题