linux下.a/.so/.la目标库区别

本文详细介绍了Linux环境下编译过程中常见的库文件类型,包括目标对象文件(.o)、静态库(.a)、动态库(.so)及libtool生成的库(.la),并通过具体实例展示了这些文件的创建过程。

转自:http://www.metsky.com/archives/604.html

在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帮助:
libtool --help

/home/yzf/env/toolchain/bin/../lib/gcc/aarch64-openwrt-linux-gnu/12.3.0/../../../../aarch64-openwrt-linux-gnu/bin/ld: warning: libm.so.6, needed by ./.libs/libxml2.so, not found (try using -rpath or -rpath-link) /home/yzf/env/toolchain/bin/../lib/gcc/aarch64-openwrt-linux-gnu/12.3.0/../../../../aarch64-openwrt-linux-gnu/bin/ld: warning: libm.so.6, needed by ./.libs/libxml2.so, not found (try using -rpath or -rpath-link) /home/yzf/env/toolchain/bin/../lib/gcc/aarch64-openwrt-linux-gnu/12.3.0/../../../../aarch64-openwrt-linux-gnu/bin/ld: ./.libs/libxml2.so: undefined reference to `log10@GLIBC_2.17' /home/yzf/env/toolchain/bin/../lib/gcc/aarch64-openwrt-linux-gnu/12.3.0/../../../../aarch64-openwrt-linux-gnu/bin/ld: ./.libs/libxml2.so: undefined reference to `pow@GLIBC_2.29' /home/yzf/env/toolchain/bin/../lib/gcc/aarch64-openwrt-linux-gnu/12.3.0/../../../../aarch64-openwrt-linux-gnu/bin/ld: ./.libs/libxml2.so: undefined reference to `fmod@GLIBC_2.38' collect2: error: ld returned 1 exit status /home/yzf/env/toolchain/bin/../lib/gcc/aarch64-openwrt-linux-gnu/12.3.0/../../../../aarch64-openwrt-linux-gnu/bin/ld: ./.libs/libxml2.so: undefined reference to `log10@GLIBC_2.17' /home/yzf/env/toolchain/bin/../lib/gcc/aarch64-openwrt-linux-gnu/12.3.0/../../../../aarch64-openwrt-linux-gnu/bin/ld: ./.libs/libxml2.so: undefined reference to `pow@GLIBC_2.29' /home/yzf/env/toolchain/bin/../lib/gcc/aarch64-openwrt-linux-gnu/12.3.0/../../../../aarch64-openwrt-linux-gnu/bin/ld: ./.libs/libxml2.so: undefined reference to `fmod@GLIBC_2.38'
11-14
l: install: warning: `../libtiff/libtiff.la' has not been installed in `/usr/lib' libtool: install: warning: `../libtiff/libtiff.la' has not been installed in `/usr/lib' libtool: install: warning: `../libtiff/libtiff.la' has not been installed in `/usr/lib' libtool: install: warning: `../libtiff/libtiff.la' has not been installed in `/usr/lib' libtool: install: warning: `../libtiff/libtiff.la' has not been installed in `/usr/lib' libtool: install: warning: `../libtiff/libtiff.la' has not been installed in `/usr/lib' libtool: install: warning: `../libtiff/libtiff.la' has not been installed in `/usr/lib' make[4] -C /home/tpuser/platform/iplatform/stable/opensource/minidlna compile /home/tpuser/board/model_fib_fg370/sdk/FG370/mtk/prebuilt/openwrt-toolchain/arm64-a55_neon-gcc-12.3.0_musl/bin/../lib/gcc/aarch64-openwrt-linux-musl/12.3.0/../../../../aarch64-openwrt-linux-musl/bin/ld: /home/tpuser/board/model_fib_fg370/sdk/FG370/openwrt/staging_dir/target-aarch64-openwrt-linux-musl_musl/usr/lib/libtiff.so: undefined reference to `jpeg_finish_compress@LIBJPEG_6.2' /home/tpuser/board/model_fib_fg370/sdk/FG370/mtk/prebuilt/openwrt-toolchain/arm64-a55_neon-gcc-12.3.0_musl/bin/../lib/gcc/aarch64-openwrt-linux-musl/12.3.0/../../../../aarch64-openwrt-linux-musl/bin/ld: /home/tpuser/board/model_fib_fg370/sdk/FG370/openwrt/staging_dir/target-aarch64-openwrt-linux-musl_musl/usr/lib/libtiff.so: undefined reference to `jpeg_set_colorspace@LIBJPEG_6.2' /home/tpuser/board/model_fib_fg370/sdk/FG370/mtk/prebuilt/openwrt-toolchain/arm64-a55_neon-gcc-12.3.0_musl/bin/../lib/gcc/aarch64-openwrt-linux-musl/12.3.0/../../../../aarch64-openwrt-linux-musl/bin/ld: /home/tpuser/board/model_fib_fg370/sdk/FG370/openwrt/staging_dir/target-aarch64-openwrt-linux-musl_musl/usr/lib/libtiff.so: undefined reference to `jpeg_destroy@LIBJPEG_6.2'
10-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值