dlsym

来源:互联网 发布:linux调整home分区大小 编辑:程序博客网 时间:2024/06/06 08:52

dlopen()函数和dlsym()函数

1 dlopen()函数

用于打开一个.so库,原型如下:

void *dlopen(const char *filename, int flag)
{
    soinfo *ret;

    pthread_mutex_lock(&dl_lock);
    ret = find_library(filename);
    if (unlikely(ret == NULL)) {
        set_dlerror(DL_ERR_CANNOT_LOAD_LIBRARY);
    } else {
        ret->refcount++;
    }
    pthread_mutex_unlock(&dl_lock);
    return ret;
}

根据filename打开一个库,成功并返回一个句柄,出现错误返回为NULL。

其中的flag:

  RTLD_LAZY 暂缓决定,等有需要时再解出符号 resolve undefined symbols as code from the dynamic library is executed

  RTLD_NOW 立即决定,返回前解除所有未决定的符号。 resolve all undefined symbols before dlopen() returns and fail if this     cannot be done

  RTLD_LOCAL

  RTLD_GLOBAL 允许导出符号

  RTLD_GROUP

  RTLD_WORLD

2 dlsym()函数

原型:

void *dlsym(void *handle, const char *symbol)

根据上面dlopen()函数放回的指针,获取函数(symbol)的地址,void*指向函数的地址,供接下来的调用。