the example of dlsym

 

void    *handle;
int     i, (*fptr)(int);


/* open the needed object */
handle = dlopen("/usr/home/me/libfoo.so", RTLD_LOCAL | RTLD_LAZY);


/* find the address of function and data objects */
*(void **)(&fptr) = dlsym(handle, "my_function");
iptr = (int *)dlsym(handle, "my_object");


/* invoke function, passing value of integer as a parameter */
(*fptr)(i);

 

有关 *(void **)(&fptr) 为什么不直接 fptr 的解释可以参考下边连接:

https://stackoverflow.com/questions/29184745/what-does-void-funcp-do-in-this-line-of-code-involving-dlsym#

 

yuchao@ubuntu:~/demo/C/glibc_demo/dlsym$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.

这个编译器用直接用 fptr 也是没问题的。

 

posted on 2018-09-13 11:55  rivsidn  阅读(116)  评论(0)    收藏  举报

导航