1. 程式人生 > >一步步完善rootfs:4.配置動態連結庫

一步步完善rootfs:4.配置動態連結庫

配置動態連結庫

一、動態連結庫作用

編譯程式可分為動態編譯和靜態編譯,靜態編譯出來的可執行程式大小相比較於動態編譯是很大的,所以為了縮小程式所佔的資源,一般使用動態編譯,動態編譯的程式在執行時會依賴動態連結庫去執行,動態連結庫會提供API給程式呼叫。

二、查詢動態連結庫

動態連結庫位置在交叉編譯工具鏈下面,可以使用find命令進行查詢,如下圖所示,我的動態連結庫就在./arm-none-linux-gnueabi/libc/lib目錄下,截圖的下方還包含了很多查詢到的內容,要自己去分辨一下。不同的編譯工具,連結庫的位置是不一樣的。

三、拷貝動態連結庫

[email protected]
:/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命令要看你的交叉編譯工具鏈,不是固定的名稱。

[email protected]:~/rootfs_zht/rootfs# arm-linux-strip lib/*so*

五、測試程式

我在ubuntu中(nfs掛載rootfs)建立資料夾、寫程式碼、編譯的過程如下:

[email protected]:~/rootfs_zht/rootfs# ls
bin  dev  etc  lib  linuxrc  proc  root  sbin  sys  tmp  usr  var
[email protected]:~/rootfs_zht/rootfs# mkdir test
[email protected]
:~/rootfs_zht/rootfs# cd test [email protected]:~/rootfs_zht/rootfs/test# vi hello.c [email protected]:~/rootfs_zht/rootfs/test# vi Makefile [email protected]:~/rootfs_zht/rootfs/test# make arm-linux-gcc hello.c -o hello_dynamic arm-linux-gcc hello.c -o hello_static -static [email protected]:~/rootfs_zht/rootfs/test# ls hello.c hello_dynamic hello_static Makefile [email protected]:~/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