配置动态链接库
一、动态链接库作用
编译程序可分为动态编译和静态编译,静态编译出来的可执行程序大小相比较于动态编译是很大的,所以为了缩小程序所占的资源,一般使用动态编译,动态编译的程序在运行时会依赖动态链接库去执行,动态链接库会提供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