动态链接库的制作与使用

简介: PC版本   点击(此处)折叠或打开 [root@localhost so_file]# ls so_test.h test_a.c test_b.c test.

PC版本


 

点击(此处)折叠或打开

  1. [root@localhost so_file]# ls
  2. so_test.h test_a.c test_b.c test.c test_c.c
  3. [root@localhost so_file]#



  4. [root@localhost so_file]# cat so_test.h
  5. #include stdio.h>

  6. void test_a();
  7. void test_b();
  8. void test_c();



  9. [root@localhost so_file]# cat test_a.c
  10. #include "so_test.h"

  11. void test_a()
  12. {
  13.     printf("This is test_a \n");
  14. }

  15. [root@localhost so_file]# cat test_b.c
  16. #include "so_test.h"

  17. void test_b()
  18. {
  19.     printf("This is test_b \n");
  20. }



  21. [root@localhost so_file]# cat test_c.c
  22. #include "so_test.h"

  23. void test_c()
  24. {
  25.     printf("This is test_c \n");
  26. }


 

编译成PC版本的so文件


 

点击(此处)折叠或打开

  1. [root@localhost so_file]# gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so
  2. [root@localhost so_file]# ls
  3. libtest.so so_test.h test_a.c test_b.c test.c test_c.c


 

使用本目录下的libtest.so


 

点击(此处)折叠或打开

  1. [root@localhost so_file]# gcc test.c -L. -ltest -o test
  2. [root@localhost so_file]# ./test
  3. This is test_a
  4. This is test_b
  5. This is test_c


 

正常使用中是将libtest.so拷贝到标准库,即/lib目录下的,操作如下:


 

点击(此处)折叠或打开

  1. [root@localhost so_file]# ls
  2. libtest.so so_test.h test test_a.c test_b.c test.c test_c.c
  3. [root@localhost so_file]# mv libtest.so /lib/
  4. [root@localhost so_file]# ls /lib/libtest.so
  5. /lib/libtest.so


 

使用自己放到标准库的libtest.so文件来编译并调用


 

点击(此处)折叠或打开

  1. [root@localhost so_file]# ls
  2. so_test.h test_a.c test_b.c test.c test_c.c
  3. [root@localhost so_file]# gcc test.c -ltest -o test
  4. [root@localhost so_file]# ls
  5. so_test.h test test_a.c test_b.c test.c test_c.c
  6. [root@localhost so_file]# ./test
  7. This is test_a
  8. This is test_b
  9. This is test_c
  10. [root@localhost so_file]#


 

ARM版本的libtest.so的编译及使用


 

点击(此处)折叠或打开

  1. [root@localhost so_file]# ls
  2. so_test.h test_a.c test_b.c test.c test_c.c
  3. [root@localhost so_file]# arm-linux-gcc test_a.c test_b.c test_c.c -fPIC -shared -o libarmtest.so
  4. [root@localhost so_file]# ls
  5. libarmtest.so so_test.h test_a.c test_b.c test.c test_c.c


 

部署到交叉编译器的标准库目录(编译ARM版本应用程序时候用到)

点击(此处)折叠或打开

  1. [root@localhost so_file]# cp libarmtest.so /opt/EmbedSky/4.3.3/arm-none-linux-gnueabi/libc/lib/
  2. [root@localhost so_file]# ls /opt/EmbedSky/4.3.3/arm-none-linux-gnueabi/libc/lib/libarmtest.so
  3. /opt/EmbedSky/4.3.3/arm-none-linux-gnueabi/libc/lib/libarmtest.so
  4. [root@localhost so_file]#

  5. [root@localhost so_file]# ls
  6. so_test.h test_a.c test_b.c test.c test_c.c
  7. [root@localhost so_file]# arm-linux-gcc test.c -larmtest -o armtest
  8. [root@localhost so_file]# ls
  9. armtest so_test.h test_a.c test_b.c test.c test_c.c
  10. [root@localhost so_file]# ./armtest
  11. bash: ./armtest: cannot execute binary file
  12. [root@localhost so_file]#


 

上面当然无法执行,需要部署到开发板上,执行效果如下:


 

点击(此处)折叠或打开

  1. [root@FORLINX6410]# ls /lib/libarmtest.so
  2. /lib/libarmtest.so
  3. [root@FORLINX6410]# ls
  4. armtest test
  5. [root@FORLINX6410]# ./armtest
  6. This is test_a
  7. This is test_b
  8. This is test_c
  9. [root@FORLINX6410]#


 

 

最主要的是GCC命令行的一个选项:
-shared 该选项指定生成动态连接库(让连接器生成T类型的导出符号表,有时候也生成弱连接W类型的导出符号),不用该标志外部程序无法连接。相当于一个可执行文件
l -fPIC:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。
l -L.:表示要连接的库在当前目录中
l -ltest:编译器查找动态连接库时有隐含的命名规则,即在给出的名字前面加上lib,后面加上.so来确定库的名称
l LD_LIBRARY_PATH:这个环境变量指示动态连接器可以装载动态库的路径。
l 当然如果有root权限的话,可以修改/etc/ld.so.conf文件,然后调用 /sbin/ldconfig来达到同样的目的,不过如果没有root权限,那么只能采用输出LD_LIBRARY_PATH的方法了。
4、注意
调用动态库的时候有几个问题会经常碰到,有时,明明已经将库的头文件所在目录 通过 “-I” include进来了,库所在文件通过 “-L”参数引导,并指定了“-l”的库名,但通过ldd命令察看时,就是死活找不到你指定链接的so文件,这时你要作的就是通过修改 LD_LIBRARY_PATH或者/etc/ld.so.conf文件来指定动态库的目录。通常这样做就可以解决库无法链接的问题了。

相关文章
|
3月前
|
定位技术 开发工具 Python
基于Python开发的玛丽大冒险小游戏(源码+可执行程序exe文件+程序配置说明书+程序使用说明书)
基于Python开发的玛丽大冒险小游戏(源码+可执行程序exe文件+程序配置说明书+程序使用说明书)
|
4月前
|
C++
[MFC] 动态链接库的制作过程和使用方法与总结
[MFC] 动态链接库的制作过程和使用方法与总结
40 0
|
5月前
|
程序员 vr&ar C语言
C/C++静态库和动态库的制作、使用、优缺点
C/C++静态库和动态库的制作、使用、优缺点
88 0
|
7月前
|
Shell
静态库和动态库制作
静态库和动态库制作
30 0
|
7月前
|
Linux 编译器 vr&ar
在linux下制作静态库和动态链接库的方法
在linux下制作静态库和动态链接库的方法
55 0
|
9月前
|
Linux vr&ar C语言
Linux下静态库和动态库(共享库)的制作和使用
Linux下静态库和动态库(共享库)的制作和使用
98 0
|
10月前
|
程序员 C++
程序员技巧 —— vs 制作自己的静态库
程序员技巧 —— vs 制作自己的静态库
47 0
|
存储 Linux 编译器
【四、静态库与动态库(共享库)】揭开链接库的神秘面纱:手把手教你制作静态链接库与动态链接库(一)
【四、静态库与动态库(共享库)】揭开链接库的神秘面纱:手把手教你制作静态链接库与动态链接库
212 0
【四、静态库与动态库(共享库)】揭开链接库的神秘面纱:手把手教你制作静态链接库与动态链接库(一)
|
Linux Shell 编译器
【四、静态库与动态库(共享库)】揭开链接库的神秘面纱:手把手教你制作静态链接库与动态链接库(二)
【四、静态库与动态库(共享库)】揭开链接库的神秘面纱:手把手教你制作静态链接库与动态链接库
275 0
【四、静态库与动态库(共享库)】揭开链接库的神秘面纱:手把手教你制作静态链接库与动态链接库(二)
|
NoSQL 编译器 vr&ar
嵌入式(十二)——库文件及静态库与动态库的制作与使用(附练习)
嵌入式(十二)——库文件及静态库与动态库的制作与使用(附练习)
160 0
嵌入式(十二)——库文件及静态库与动态库的制作与使用(附练习)