博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C语言下动态库相互调用
阅读量:6900 次
发布时间:2019-06-27

本文共 1753 字,大约阅读时间需要 5 分钟。

前段时间需要完成多个模块业务,而这些模块的接口都是一样的,于是为了方便管理就把每个模块都根据接口封装成了SO库,这里就留下SO库调用样例

 

SO库源文件代码:

1 //TestSo.c2 #include 
3 4 int CommonInterface(char* str, unsigned int strLen)5 {6 //deal with ...7 printf("%s, %d\n", str, strLen);8 return 1;9 }

 

编译命令:

gcc -shared -o TestSo.so TestSo.c
 
使用库代码:
1 //Main.c 2 #include 
3 #include
4 5 typedef int (*CommonInterface_Func)(char* str, unsigned int strLen); 6 7 #define TEST_SO_FILE "/root/test/libtestSo.so" 8 void* soHandle = NULL; 9 10 /* get dynamic library function address, returns 1 success, otherwise fail. */11 int getCommonInterfaceFuncAddr(CommonInterface_Func* funcAddr)12 {13 // laod dynamic library.14 soHandle = dlopen(TEST_SO_FILE, RTLD_LAZY);15 if ( soHandle == NULL )16 {17 printf("dlopen,(%s)", dlerror());18 return 0;19 } 20 // get func addr21 *funcAddr = (CommonInterface_Func)dlsym(soHandle, "CommonInterface");22 if ( *funcAddr == NULL )23 {24 printf("dlsym,(%s)", dlerror());25 dlclose(m_soHandle);26 return 0;27 }28 return 1;29 }30 31 int main(int argc, char* argv[])32 {33 char* str = "hello so.";34 CommonInterface_Func funcAddr = NULL;35 int result = getCommonInterfaceFuncAddr(&funcAddr);36 37 if (result != 1)38 {39 printf("get func address fail.\n");40 return 0;41 }42 printf("get func address success.\n");43 result = funcAddr(str, strlen(str));44 printf("exec func result : %d\n", result);45 46 return 1;47 }

 

编译命令:

 gcc -o Main_exec Main.c -ldl

 

 执行:./Main_exec

 

参考链接:http://www.cnblogs.com/leaven/archive/2010/06/11/1756294.html

转载于:https://www.cnblogs.com/1024Planet/p/4425242.html

你可能感兴趣的文章
51nod 1301 集合异或和——异或dp
查看>>
weblogic.jdbc.wrapper.Blob_oracle_sql_BLOB cannot be cast to oracle.sql.BLOB 解决方法
查看>>
表格边框设置
查看>>
问题 K: A/B Problem
查看>>
Django实战(7):改造ProductList界面
查看>>
大专生自学Java到找到工作的心得
查看>>
CI框架
查看>>
python下使用protobuf
查看>>
少搞一点 对象, 多搞一点 文本
查看>>
首页logo的代码标志性写法,方便SEO
查看>>
安装完vs2008中文的sp1后,智能提示变成英文.
查看>>
Scala.Actor实践心得与设计思想
查看>>
代码可读性的改良
查看>>
网页调试:myeclipse修改javascript代码后,执行没有变化呀
查看>>
Linux使用pam_tally2.so模块限制登录失败锁定时间
查看>>
搭建Docker集群测试环境--swarm、docker-compose、portainer
查看>>
UVA 12167 Proving Equivalences 强连通分量
查看>>
python 之字符编码
查看>>
jquery操作select(增加,删除,清空)
查看>>
Ruby 交互式编程工具——irb
查看>>