您需要编写代码:
// file ds.c #include <stdio.h> #include <stdlib.h> #include <dlfcn.h> void hello () { printf ("hello worldn"); } int main (int argc, char **argv) { char *buf = "hello"; void *hndl = dlopen (NULL, RTLD_LAZY); if (!hndl) { fprintf(stderr, "dlopen failed: %sn", dlerror()); exit (EXIT_FAILURE); }; void (*fptr) (void) = dlsym (hndl, buf); if (fptr != NULL) fptr (); else fprintf(stderr, "dlsym %s failed: %sn", buf, dlerror()); dlclose (hndl); }仔细阅读dlopen(3),始终检查那里的
dlopen&
dlsym函数是否成功,并
dlerror在失败时使用。
并用编译上面的
ds.c文件
gcc -std=c99 -Wall -rdynamic ds.c -o ds -ldl
不要忘记
-Wall获得所有警告和
-rdynamic标志(以便能够将
dlsym自己的符号放入动态表中)。
在我的Debian / Sid /
x86-64系统上(
gcc版本4.8.2和
libc6版本2.17-93提供了
-ldl我编译的内核3.11.6,
binutils软件包2.23.90提供了
ld),执行
./ds给出了预期的输出:
% ./ds hello world
乃至:
% ltrace ./ds __libc_start_main(0x4009b3, 1, 0x7fff1d0088b8, 0x400a50, 0x400ae0 <unfinished ...> dlopen(NULL, 1) = 0x7f1e06c9e1e8 dlsym(0x7f1e06c9e1e8, "hello") = 0x004009a0 puts("hello world"hello world ) = 12 dlclose(0x7f1e06c9e1e8) = 0 +++ exited (status 0) +++


