动态链接库的制作与调用

36 篇文章 0 订阅
 

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

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

void test_a();
void test_b();
void test_c();

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

void test_a()
{
    printf("This is test_a \n");
}

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

void test_b()
{
    printf("This is test_b \n");
}

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

void test_c()
{
    printf("This is test_c \n");
}

编译成PC版本的so文件

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

使用本目录下的libtest.so

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

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

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

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

[root@localhost so_file]# ls
so_test.h  test_a.c  test_b.c  test.c  test_c.c
[root@localhost so_file]# gcc test.c -ltest -o test
[root@localhost so_file]# ls
so_test.h  test  test_a.c  test_b.c  test.c  test_c.c
[root@localhost so_file]# ./test
This is test_a
This is test_b
This is test_c
[root@localhost so_file]#

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

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

部署到交叉编译器的标准库目录(编译ARM版本应用程序时候用到)
[root@localhost so_file]# cp libarmtest.so /opt/EmbedSky/4.3.3/arm-none-linux-gnueabi/libc/lib/
[root@localhost so_file]# ls /opt/EmbedSky/4.3.3/arm-none-linux-gnueabi/libc/lib/libarmtest.so
/opt/EmbedSky/4.3.3/arm-none-linux-gnueabi/libc/lib/libarmtest.so
[root@localhost so_file]#

[root@localhost so_file]# ls
so_test.h  test_a.c  test_b.c  test.c  test_c.c
[root@localhost so_file]# arm-linux-gcc test.c -larmtest -o armtest
[root@localhost so_file]# ls
armtest  so_test.h  test_a.c  test_b.c  test.c  test_c.c
[root@localhost so_file]# ./armtest
bash: ./armtest: cannot execute binary file
[root@localhost so_file]#

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

[root@FORLINX6410]# ls /lib/libarmtest.so
/lib/libarmtest.so
[root@FORLINX6410]# ls
armtest  test
[root@FORLINX6410]# ./armtest
This is test_a
This is test_b
This is test_c
[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文件来指定动态库的目录。通常这样做就可以解决库无法链接的问题了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值