一步步完善rootfs:4.配置动态链接库

一、动态链接库作用

编译程序可分为动态编译和静态编译,静态编译出来的可执行程序大小相比较于动态编译是很大的,所以为了缩小程序所占的资源,一般使用动态编译,动态编译的程序在运行时会依赖动态链接库去执行,动态链接库会提供API给程序调用。

二、查找动态链接库

动态链接库位置在交叉编译工具链下面,可以使用find命令进行查找,如下图所示,我的动态链接库就在./arm-none-linux-gnueabi/libc/lib目录下,截图的下方还包含了很多查找到的内容,要自己去分辨一下。不同的编译工具,链接库的位置是不一样的。

三、拷贝动态链接库

root@ubuntu:/usr/local/arm/arm-2009q3/arm-none-linux-gnueabi/libc/lib# cp *so* /root/rootfs_zht/rootfs/lib/ -rdf

动态链接库下面有好多是链接文件,所以执行cp的时候注意加上-rdf选项,操作执行完之后进入开发板就可以看到如下图所示:

四、缩小链接库体积

动态链接库so文件中包含了调试信息,这些调试信息在程序运行时是没用的,并且这些符号会占用一定空间,在传统的嵌入式系统中flash空间是有限的,所以为了节省空间常常把这些调试信息去掉。
去掉调试信息的命令如下,arm-linux-strip命令要看你的交叉编译工具链,不是固定的名称。

root@ubuntu:~/rootfs_zht/rootfs# arm-linux-strip lib/*so*

五、测试程序

我在ubuntu中(nfs挂载rootfs)创建文件夹、写代码、编译的过程如下:

root@ubuntu:~/rootfs_zht/rootfs# ls
bin  dev  etc  lib  linuxrc  proc  root  sbin  sys  tmp  usr  var
root@ubuntu:~/rootfs_zht/rootfs# mkdir test
root@ubuntu:~/rootfs_zht/rootfs# cd test
root@ubuntu:~/rootfs_zht/rootfs/test# vi hello.c
root@ubuntu:~/rootfs_zht/rootfs/test# vi Makefile
root@ubuntu:~/rootfs_zht/rootfs/test# make
arm-linux-gcc hello.c -o hello_dynamic
arm-linux-gcc hello.c -o hello_static -static
root@ubuntu:~/rootfs_zht/rootfs/test# ls
hello.c  hello_dynamic  hello_static  Makefile
root@ubuntu:~/rootfs_zht/rootfs/test# 

hello.c的内容:

#include <stdio.h>

void main()
{
        printf("hello world.\n");
}

Makefile的内容:

all:
        arm-linux-gcc hello.c -o hello_dynamic
        arm-linux-gcc hello.c -o hello_static -static

clean:
        rm hello_dynamic hello_static

开发板上执行的结果:


参考资料: 朱老师嵌入式Linux

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值