云网牛站
所在位置:首页 > Linux常用命令大全 > 更多实用命令 > libtool命令

libtool命令

以下是libtool命令的使用详解

一.libtool的作用

offer a standard procedure for creating shared libraries on different platforms
libtool 是一个通用库支持脚本,将使用动态库的复杂性隐藏在统一、可移植的接口中,也就是说,你可以在不同平台上创建并调用动态库,我们可以认为libtool是gcc的一个抽象,也就是说,它包装了gcc或者其他的任何编译器,用户无需知道细节,只要告诉libtool说我需要要编译哪些库即可,并且,它只与libtool文件打交道,例如lo、la为后缀的文件。

二.libtool的使用

1.Creating object files

[root@localhost ~]# libtool --mode=compile gcc -g -O -c foo.c
gcc -g -O -c foo.c -fPIC -DPIC -o .libs/foo.o
gcc -g -O -c foo.c -o foo.o >/dev/null 2>&1
# libtool --mode=compile gcc -g -O -c hello.c
gcc -g -O -c hello.c -fPIC -DPIC -o .libs/hello.o
gcc -g -O -c hello.c -o hello.o >/dev/null 2>&1

【说明】libtool编译出两个版本的relocatable object,一个是fPIC(位置无关的),放在.libs目录下;另一个则是普通的,放在本地。

2.linking shared library

[root@localhost ~]# libtool --mode=link --tag=CC gcc -g -O -o libhello.la -rpath /usr/local/lib foo.lo
rm -fr .libs/libhello.a .libs/libhello.la .libs/libhello.lai .libs/libhello.so libs/libhello.so.0 .libs/libhello.so.0.0.0
gcc -shared .libs/foo.o   -Wl,-soname -Wl,libhello.so.0 -o .libs/libhello.so.0.0.0
(cd .libs && rm -f libhello.so.0 && ln -s libhello.so.0.0.0 libhello.so.0)
(cd .libs && rm -f libhello.so && ln -s libhello.so.0.0.0 libhello.so)
ar cru .libs/libhello.a foo.o
ranlib .libs/libhello.a
creating libhello.la
(cd .libs && rm -f libhello.la && ln -s ../libhello.la libhello.la)

【说明】link出两个共享库,一个是static,一个则是dynamic;需要注意的是,-rpath必须有才能产生dynamic库来,如果用-static,则只创建static库。

ranlib的作用

On some older UNIX systems, ranlib added a table of contents to archive libraries, which converted each archive to a form. that could be linked more rapidly. This is no longer needed as the ar command automatically provides all the functionality ranlib used to provide.
在一些旧版本的系统上,ranlib负责把静态库转换为其他的某种格式,使得新的库能够更快的链接;现在ar命令已经包含了上述功能;
This command is provided as a convenience for software developers who need to maintain Makefiles that are portable across a variety of operating systems.
为了兼容性,在makefile中还是保留ranlib

3.install shared library

[root@localhost ~]# libtool --mode=install cp libhello.la /usr/local/lib/libhello.la
[root@localhost ~]# libtool --mode=install install -c libhello.la /usr/local/lib/libhello.la

两个命令都可以,效果相同

4.linking executable file

[root@localhost ~]# libtool --mode=link gcc -g -O -o hello hello.lo -rpath /usr/local/lib libhello.la
gcc -g -O -o .libs/hello .libs/hello.o ./.libs/libhello.so

creating hello-rpath项负责添加运行时库路径,否则只能手工修改LD_LIBRARY_PATH环境变量了。验证一下:

[root@localhost ~]# ldd .libs/hello
linux-gate.so.1 => (0xffffe000)
libhello.so.0 => /usr/local/lib/libhello.so.0 (0x40019000)
libc.so.6 => /lib/tls/libc.so.6 (0x40031000)
/lib/ld-linux.so.2 (0x40000000)

5.install executable file     

[root@localhost ~]# libtool --mode=install cp hello /usr/local/bin/hello

安装可执行程序。

6.运行

[root@localhost ~]# libtool --mode=execute hello

或直接运行hello注意:此处hello已经安装在/usr/local/bin下了,可以用which hello来查看。

相关命令