Linux动态加载库的使用方法
2015-11-09 21:58
465 查看
// string.c #define ENDSTRING '\0' int StrLen(char *string) { int len = 0; while (*string++ != ENDSTRING) { len++; } return len; } // main.c #include <stdio.h> #include <dlfcn.h> int main(int argc, char *argv[]) { char src[] = "Hello Dymatic"; int (*pStrLenFun)(char *str); void *phandle = NULL; char *perr = NULL; phandle = dlopen("./libstr.so", RTLD_LAZY); if (!phandle) { printf("Failed Load library!\n"); } perr = dlerror(); if (perr != NULL) { printf("%s\n", perr); return 0; } pStrLenFun = dlsym(phandle, "StrLen"); perr = dlerror(); if (perr != NULL) { printf("%s\n", perr); return 0; } printf("the string length is: %d\n", pStrLenFun(src)); dlclose(phandle); return 0; } 编译生成 libstr.so:gcc -shared string.c -o libstr.so 编译 main.c 生成 test:gcc -o test main.c -ldl // 需要动态链接库 libdl.so
相关文章推荐
- Linux内核(驱动)常用函数
- linux strace追踪mysql执行语句 (mysqld --debug)
- Linux多线程实践(2) --线程基本API
- 常见的Linux指令与Android的指令
- linux文件权限与目录配置
- sinlinx独家发布Linux-Qt 之Linux软件的编译
- linux新增特性timerfd
- 父子进程终止顺序与僵死进程
- Centos7+LVS+Keepalived实现Exchange2016高可用性
- 全面解析Linux 内核 3.10.x - 编译前的准备
- Linux 查看设置系统语言
- Linux ssh远程登录
- 【鸟哥的linux私房菜-学习笔记】进程管理
- linux安装sqlcmd登录sqlserver
- linux使用FIO测试磁盘的iops
- Linux下system和exec函数族的区别
- Linux进程间通信——使用命名管道
- linux下监控是否有IO
- Linux进程间通信——使用匿名管道
- linux gdb调试