参考 http://www.cnblogs.com/likwo/archive/2012/05/09/2492225.html
静态库为程序链接时加载,共享库为程序运行时加载(可以给多个程序共享,多个程序维护一份内存副本);动态链接可以在程序运行过程中的任何时间加载,特别适合在函数中加载一些模块和Plugin扩展模块的场合(需要时才加载,否则不需要)。
加载动态库的相关函数及使用步骤为:通过一个API来打开一个函数库dlopen(),寻找符号表void * dlsym(void *handle, char *symbol); 处理错误dlerror()和关闭函数表dlclose()。 例子如下:
#include<stdio.h>
#include<dlfcn.h>
int main(int argc, char **argv)
{
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen ("/lib/libm.so.6", RTLD_LAZY); //RLTD_LAZY表示懒加载(用的的符号表项才会加载),还有其他的选项如RLTD_NOW,表示立即加载,等等
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL)
{
fputs(error, stderr);
exit(1);
}
printf ("%f ", (*cosine)(2.0));
dlclose(handle);
}
如果这个程序名字叫foo.c,那么用下面的命令来编译: gcc -o foo foo.c -ldl (ldl是动态库加载函数的链接库)
更多帮助可以执行下man dlopen ,上面的四个函数都有详细说明