测试同一个动态链接库重复dlopen是否会更新

来源:互联网 发布:windows几的系统最好 编辑:程序博客网 时间:2024/06/03 12:34

主函数文件main_dl.c:

#include <stdio.h>#include <dlfcn.h>#include <unistd.h>#include <time.h>int main(){    void (*func)();    void *handle = NULL;    char *myso = "./mylib.so";    while(1)    {        sleep(1);        if((handle = dlopen(myso, RTLD_NOW)) != NULL) {            printf("success\n");            func = (void(*)())dlsym(handle, "__printf");            if(func != NULL)(*func)();            else printf("dlsym - %s\n", dlerror());            //dlclose(handle);        }        else printf("dlopen - %s\n", dlerror());    }}

编译:gcc main_dl.c -ldl -rdynamic


链接库文件mylib.c:

#include <stdio.h>void  __printf() {    printf("version 2\n");}
编译:gcc -fPIC -shared -o mylib.so mylib.c


验证证明,如果不dlclose,程序重复dlopen并不会更新加载的链接库文件。


0 0