在linux平臺上編譯時,常會遇到目標庫的疑問,有靜態庫也有動態庫,單個理解都不太難,但是對復雜的工程而言,一旦混合到一起去,對整個工程的理解和調用,將會造成很大困擾,本文就匯總這幾種常見編譯結果文件的區別。
一、格式說明
linux下編譯,常會遇到后綴為:.o .so .a .la .ko等格式文件,盡管linux并不以擴展名作為識別文件格式的唯一依據,但規范約定還是有的,如下:
- .o 是目標對象文件,相當于windows中的.obj文件
- .a 為靜態庫,可以是一個或多個.o合在一起,用于靜態連接
- .la 為libtool生成的共享庫,其實是個配置文檔。可以用$file *.la查看*.la文件,或用vi來查看。
- .so 為共享庫,類似windows平臺的dll文件
補充: 還有一種擴展名為.ko 文件,不過它是Linux內核使用的動態鏈接文件后綴,屬于模塊文件,用來在Linux系統啟動時加載內核模塊。
二、創建實例
1、創建.o對象文件
$ gcc -c test.c
生成test.o,跳過鏈接對象,所以不是可執行文件。
2、創建.a靜態庫文件
$ ar -r libtest.a test1.o test2.o
3、創建動態庫.so
$ gcc -Wall -fpic -shared test1.c test2.c -o libtest.so
上一句執行,將test1.c和test2.c編譯生成動態庫文件libtest.so
4、鏈接庫文件
$ gcc -Wall -fpic -shared -Ltest test3.c -o libtest.so
編譯test3.c后并與靜態libtest.a鏈接(默認會到/usr/lib下找該文件)生成libtest.so動態庫。
5、生成.la庫
.la庫一般通過makefile進行,當然也可以通過命令行進行,參考命令:
$libtool --mode=link gcc -o libmylib.la -rpath /usr/lib –L/usr/lib –la
libtool將會搜索libmylib.a文件,并傳家libmylib.la。更多libtool幫助如下:
Tst@Tst-PC /cygdrive/d/ $ libtool --help Usage: libtool [OPTION]... [MODE-ARG]...Provide generalized library-building support services.--config show all configuration variables --debug enable verbose shell tracing -n, --dry-run display commands without modifying any files --features display basic configuration information and exit --mode=MODE use operation mode MODE --preserve-dup-deps don't remove duplicate dependency libraries --quiet, --silent don't print informational messages --no-quiet, --no-silent print informational messages (default) --tag=TAG use configuration variables from tag TAG -v, --verbose print more informational messages than default --no-verbose don't print the extra informational messages --version print version information -h, --help, --help-all print short, long, or detailed help messageMODE must be one of the following:clean remove files from the build directory compile compile a source file into a libtool object execute automatically set library path, then run a program finish complete the installation of libtool libraries install install libraries or executables link create a library or an executable uninstall remove libraries from an installed directoryMODE-ARGS vary depending on the MODE. When passed as first option, `--mode=MODE' may be abbreviated as `MODE' or a unique abbreviation of that. Try `libtool --help --mode=MODE' for a more detailed description of MODE.When reporting a bug, please describe a test case to reproduce it and include the following information:host-triplet: i686-pc-cygwin shell: /bin/sh compiler: gcc compiler flags: -g -O2 -pipe linker: /usr/i686-pc-cygwin/bin/ld.exe (gnu? yes) libtool: (GNU libtool) 2.4 automake: automake (GNU automake) 1.11.1 autoconf: autoconf (GNU Autoconf) 2.68Report bugs to <bug-libtool@gnu.org>. GNU libtool home page: <http://www.gnu.org/software/libtool/>. General help using GNU software: <http://www.gnu.org/gethelp/>.
參考資料:
http://wiki.ubuntu.org.cn/index.php?title=Compiling_C&variant=zh-hans
http://afreez.blog.51cto.com/59057/7338
http://blog.csdn.net/lhf_tiger/article/details/7065289
http://topic.csdn.net/u/20080124/13/c34ece2c-81a0-4818-bec4-d705aa4aecb1.html