dlopen、dlsym和dlclose的使用
来源:互联网 发布:2017淘宝收藏有礼 编辑:程序博客网 时间:2024/05/29 07:34
因为项目需求,需要集成一个第三方的方案。但是第三方只给我们release 了一个so 库+函数说明;现在需要调用到so 库中的函数。。
先给出一些函数的定义
#include <dlfcn.h> void *dlopen(const char *filename, int flag); char *dlerror(void); void *dlsym(void *handle, const char *symbol); int dlclose(void *handle); //Link with -ldl.
这里需要注意亮点
1、加上头文件#inclue<dlfcn.h>
2、编译的时候加上 -ldl选项
下面是生成动态供调用的so库代码
#include<stdio.h>#include<unistd.h>#include<sys/types.h>//#define bool intvoid *getValue(char *param,int intparam){ printf("%s-->%d,param=%s,intparam=%d\n",__FUNCTION__,__LINE__,param,intparam); return 0;}void *getValue1(char *param,int intparam,int isReturnAddress){ printf("%s-->%d,param=%s,intparam=%d,isReturnAddress=%d\n",__FUNCTION__,__LINE__,param,intparam,isReturnAddress); if(isReturnAddress>0){ return 1; }else{ return 0; }}void * InitEffect(unsigned char bitsPerSample,unsigned char numChannels,unsigned char dataIsInterleaved,unsigned int sampleRate){ printf("%s-->%d\n",__FUNCTION__,__LINE__);}void *ReinitEffect(unsigned char bitsPerSample,unsigned char numChannels,unsigned char dataIsInterleaved,unsigned int sampleRate){ printf("%s-->%d\n",__FUNCTION__,__LINE__);}int Process(void* inBuffer,void* outBuffer,void *mePtr,unsigned short num_frames){ printf("%s-->%d\n",__FUNCTION__,__LINE__);}int SetParam(void *mPtr,int paramId,void* paramVal){ printf("%s-->%d\n",__FUNCTION__,__LINE__);}int GetParam(void *mPtr,int paramId){ printf("%s-->%d\n",__FUNCTION__,__LINE__);}int Deallocate(void * mePtr){ printf("%s-->%d\n",__FUNCTION__,__LINE__);}void * GetSpecBufAddress(void *mePtr,int *bufLength){ printf("%s-->%d\n",__FUNCTION__,__LINE__);}
编译生成 so库
gcc -fPIC -shared -o test.so test.c务必注意-fPIC编译选择
测试使用so库的代码
#include<stdio.h>#include<unistd.h>#include<sys/types.h>#include <dlfcn.h>typedef void* (*TCT_InitEffect)(unsigned char,unsigned char,unsigned char,unsigned int);typedef void* (*TCT_ReinitEffect)(unsigned char,unsigned char,unsigned char,unsigned int);typedef int (*TCT_Process)(void*,void*,void *,unsigned short);typedef int (*TCT_SetParam)(void*,int,void*);typedef int (*TCT_GetParam)(void*,int);typedef int (*TCT_Deallocate)(void*);typedef int (*TCT_GetSpecBufAddress)(void *mePtr,int *bufLength);typedef void* (*tct_getValue)(char*,int);typedef void* (*tct_getValue1)(char*,int,int);int main(int argc, char **argv) { //open the .so library void* handle = dlopen("./test.so", RTLD_LAZY); //get function pointer from dlsym tct_getValue getValue = (tct_getValue)dlsym(handle, "getValue"); tct_getValue1 getValue1 = (tct_getValue1)dlsym(handle, "getValue1"); TCT_InitEffect Harman_InitEffect = (TCT_InitEffect)dlsym(handle, "InitEffect"); TCT_ReinitEffect Harman_ReinitEffect = (TCT_ReinitEffect)dlsym(handle, "ReinitEffect"); TCT_Process Harman_Process = (TCT_Process)dlsym(handle, "Process"); TCT_SetParam Harman_SetParam = (TCT_SetParam)dlsym(handle, "SetParam"); TCT_GetParam Harman_GetParam = (TCT_GetParam)dlsym(handle, "GetParam"); TCT_Deallocate Harman_Deallocate = (TCT_Deallocate)dlsym(handle, "Deallocate"); TCT_GetSpecBufAddress Harman_GetSpecBufAddress = (TCT_GetSpecBufAddress)dlsym(handle, "GetSpecBufAddress"); //call function Harman_InitEffect(1,1,1,1); Harman_ReinitEffect(1,1,1,1); Harman_Process(1,1,1,1); Harman_SetParam(1,1,1); Harman_GetParam(1,1); Harman_Deallocate(1); Harman_GetSpecBufAddress(1,1); getValue("getValuexxxxxxx",1); int xxx = getValue1("getValue1",1,0); printf("main function return from getVaule1 =%d\n",xxx); //dlclose handle dlclose(handle); return 0;}
</pre><p></p><pre>编译生成可执行文件
gcc hello.c -o hello -ldl这里需要加入-ldl编译选项
执行
./hello运行结果
InitEffect-->21ReinitEffect-->24Process-->28SetParam-->31GetParam-->34Deallocate-->37GetSpecBufAddress-->40getValue-->7,param=getValuexxxxxxx,intparam=1getValue1-->12,param=getValue1,intparam=1,isReturnAddress=0main function return from getVaule1 =0
0 0
- dlopen、dlsym和dlclose的使用
- dlopen,dlsym,dlclose的使用
- dlopen、dlsym和dlclose的使用和举例
- dlopen、dlsym和dlclose的使用和举例
- dlopen、dlsym和dlclose的使用和举例
- dlopen、dlsym和dlclose的使用和举例
- dlopen、dlsym和dlclose的使用和举例
- 動態鏈接庫的使用-dlopen dlsym dlclose dlerror
- linux下dlopen的使用 dlopen dlsym dlclose解析
- 解决cppunit编译错误,未定义的dlsym,dlopen和dlclose
- 动态库的动态链接、打开、调用,使用dlopen、dlsym和dlclose
- linux下so库的加载和使用(dlopen/dlsym/dlclose)
- Linux系统下的dlopen、dlsym、dlclose
- dlopen dlsym dlclose解析
- dlopen dlsym dlclose dlerror
- dlopen dlsym dlclose dlerror
- dlopen;dlclose;dlsym
- dlopen dlsym dlclose解析
- windows平台使用putty工具集中pscp上传文件夹
- 【ruby学习笔记】Hello World
- HDOJ 题目1595 find the longest of the shortest(枚举,最短路记录路径)
- Android UI-自定义日历控件
- 杂七杂八 - 新年
- dlopen、dlsym和dlclose的使用
- ssh连接服务器出现Agent admitted failure to sign using the key错误
- Android SDK Manager 无法下载更新,或者更新速度超慢,或者待安装包列表不显示!
- Histogram of Oriented Gradients for Human Detection 翻译
- arm-linux-gcc/ld/objcopy/objdump参数总结
- 第三章思维导图
- C内存管理一 概述
- Clock Synchronization in Distributed Real Time Systems
- 数据库复习