dlsym使用实例

论坛 期权论坛 脚本     
匿名网站用户   2020-12-21 07:49   67   0

#define LIB_FULL_NAME "/system/lib/libipod.so"

void (*libipod_exit)(int reason) = NULL;
static void (*libipod_setup)(struct ipod_param *) = NULL;
void (*libipod_log)(const char *fmt, ...) = NULL;

void loadlib()

{
void *handle, * func;

handle = dlopen(LIB_FULL_NAME, RTLD_NOW);
if (handle == NULL) {
ipod_log("Can't load library: %s", dlerror());
goto fail_out;
}

if ((libipod_setup = (void (*)(struct ipod_param *))dlsym(handle, "libipod_setup")) == NULL) {
ipod_log("load 'libipod_setup' error: %s", dlerror());
goto close_handle;
}

if ((libipod_exit = (void (*)(int))dlsym(handle, "exit_ipod")) == NULL) {
ipod_log("exit_ipod error: %s", dlerror());
goto close_handle;
}

// logging into kernel log & /data/misc/ipod_log
if ((libipod_log = (void (*)(const char *, ...))dlsym(handle, "ipod_log")) == NULL) {
ipod_log("libipod_log error: %s", dlerror());
goto close_handle;
}

ipod_log("loadlib success!");
return;

close_handle:
dlclose(handle);
fail_out:
do_shutdown("fail to load libipod", false);

}

int main(int argc, char *argv[])
{
loadlib();

......

if(libipod_setup){
libipod_setup(&ipod_param);
if(ipod_param.fast_reboot)
exit_ipod(EXIT_POWER_UP);
}else{

}

}

在main中,通过loadlib()获取到符号libipod_setup、libipod_exit和libipod_log对应的函数地址,进而使用库libipod.so中定义的函数实现。

为了使程序方便扩展,具备通用性,linux提供了加载和处理动态链接库的系统调用。采用异步事件驱动模型,保证主程序逻辑不变,将各个业务以动态链接库的形式加载进来。


参考资料:

1)http://www.cnblogs.com/Anker/p/3746802.html

2)http://blog.csdn.net/ccskyer/article/details/5733508

3)http://blog.csdn.net/bailyzheng/article/details/17613847


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP