分类: LINUX
2013-09-01 14:46:00
[bill@billstone make_lib]$ cat main.c
#include
int main()
{
void *pHandle;
void (*pFunc)(); // 指向函数的指针
int *p;
pHandle = dlopen("./d1.so", RTLD_NOW); // 打开动态库
if(!pHandle){
printf("Can't find d1.so \n");
exit(1);
}
pFunc = (void (*)())dlsym(pHandle, "print"); // 获取库函数 print 的地址
if(pFunc)
pFunc();
else
printf("Can't find function print\n");
p = (int *)dlsym(pHandle, "p"); // 获取库变量 p 的地址
if(p)
printf("p = %d\n", *p);
else
printf("Can't find int p\n");
dlclose(pHandle); // 关闭动态库
return 0;
}