【Linux】libtool的介紹及其基本用法

一、 libtool介紹


GNU Libtool 可以容易的在不同的系統中建立動態鏈接庫。它通過一個稱爲 Libtool 庫的抽象,
隱藏了不同系統之間的差異,給開發人員提供了一致的的接口。


二、 libtool編譯


1.  網站上下載libtool的源碼包

2. 解壓源碼包之後,進入包目錄,執行以下命令

./configure --prefix=/home/lizj/0002_linux/libtool/libtool_install  # 該路徑依據你自己的系統設置

make && make install


三、基本用法


1. 創建庫文件fun.c,內容如下

#include <stdio.h>
#include <string.h>

int testFunction_01(char *str)
{
	printf("[%s] : str[%s]\n", __FUNCTION__,str);
	return 0;
}


2. 創建對象: libtool --mode=compile gcc -c fun.c


[root@f8s test_02]# libtool --mode=compile gcc -c fun.c 

mkdir .libs
 gcc -c fun.c  -fPIC -DPIC -o .libs/fun.o
 gcc -c fun.c -o fun.o >/dev/null 2>&1
[root@f8s test_02]# 
[root@f8s test_02]# 
[root@f8s test_02]# ls
fun.c  fun.lo  fun.o
[root@f8s test_02]# </span>
生成的funlo文件,裏面記錄了建立動態鏈接庫和靜態鏈接庫分別所需要的真實文件名稱

後面 Libtool 將使用這個文件而不是直接的使用 .libs/fun.o 和 fun.o


3. 創建庫: gcc -o libfun.la fun.lo -rpath /tmp

[root@f8s test_02]# libtool --mode=link gcc -o libfun.la fun.lo -rpath ./tmp
libtool: link: only absolute run-paths are allowed
[root@f8s test_02]# libtool --mode=link gcc -o libfun.la fun.lo -rpath /tmp 
gcc -shared  .libs/fun.o   -Wl,-soname -Wl,libfun.so.0 -o .libs/libfun.so.0.0.0
(cd .libs && rm -f libfun.so.0 && ln -s libfun.so.0.0.0 libfun.so.0)
(cd .libs && rm -f libfun.so && ln -s libfun.so.0.0.0 libfun.so)
ar cru .libs/libfun.a  fun.o
ranlib .libs/libfun.a
creating libfun.la
(cd .libs && rm -f libfun.la && ln -s ../libfun.la libfun.la)
[root@f8s test_02]# 
[root@f8s test_02]# ls
fun.c  fun.lo  fun.o  libfun.la	
.la 是 Libtool 的庫文件後綴,Libtool 希望後續使用 libfoo.la 文件而不是直接使用 libfun.a 和 libfun.so 文件


4. 安裝庫:libtool --mode=install install -c libfun.la /tmp

[root@f8s tmp]# ls lib*
libfun.a  libfun.la  libfun.so  libfun.so.0  libfun.so.0.0.0
libtool -n --mode=finish /tmp   # 需要做一些配置才能正確使用

5. 創建源文件main.c,內容如下所示,如何執行libtool --mode=compile gcc -c main.c

#include <stdio.h>
#include <string.h>

extern int testFunction_01(char *str);

int main(void)
{
	testFunction_01("hello libtool");
	return 0;
}


6. 使用動態庫:libtool --mode=link gcc -o main main.lo /tmp/libfun.la


7. 使用靜態庫: libtool --mode=link  gcc  -o main main.lo /tmp/libfun.la -static-libtool-libs


8. 執行結果

[root@f8s test_02]# ./main

[testFunction_01] : str[hello libtool]
9. 卸載庫:libtool --mode=uninstall rm /tmp/libfun.la
[root@f8s test_02]# libtool --mode=uninstall rm /tmp/libfun.la 
rm /tmp/libfun.la /tmp/libfun.so.0.0.0 /tmp/libfun.so.0 /tmp/libfun.so /tmp/libfun.a


四、如何更改wifidog依賴的libhttpd.so的庫名稱


1. -修改libhttpd/Makefile.am  Makefile.in 將名稱libhttpd.la修改爲libhttpd_lizijun.la

2.  修改src/Makefile.amMakefile.in  將名稱libhttpd.la修改爲libhttpd_lizijun.la


五、 參考鏈接


1. 代碼下載: 點擊打開鏈接

2. 參考文檔: 點擊打開鏈接


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章