The examples in this chapter introduce the main features of libtool by comparing the standard library building procedure to libtool’s operation on two different platforms:
‘a23’ An Ultrix 4.2 platform with only static libraries. --静态库机器环境
‘burger’ A NetBSD/i386 1.2 platform with shared libraries --动态库
Shared libraries, however, may only be built from position-independent code (PIC). So, special flags must be passed to the compiler to tell it to generate PIC rather than the standard position-dependent code.
Since this is a library implementation detail, libtool hides the complexity of PIC compiler flags and uses separate library object files (the PIC one lives in the ‘ .libs’ subdirectory and the static one lives in the current directory). On systems without shared libraries, the PIC library object files are not created, whereas on systems where all code is PIC, such as AIX, the static ones are not created.
静态库的编译:
a23$ libtool --mode=compile gcc -g -O -c foo.c
相当于: gcc -g -O -c foo.c -o foo.o
a23$ libtool --mode=compile gcc -g -O -c hello.c
相当于: gcc -g -O -c hello.c -o hello.o
The ‘.lo’ file is the libtool object, which Libtool uses to determine what object file may be built into a shared library.
可以查看一下.lo 包含编译动态库需要的.o文件
动态库的编译:
On shared library systems, libtool automatically generates an additional PIC object by inserting the appropriate PIC generation flags into the compilation command: burger$ libtool --mode=compile gcc -g -O -c foo.c
相当于:mkdir .libs 建立隐藏目录.libs 把用于产生动态库需要的 .o 文件放入.lib目录
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
Note that Libtool automatically created ‘.libs’ directory upon its first execution, where PIC library object files will be stored.
在查看一些 .lo :
# foo.lo - a libtool object file
# Generated by ltmain.sh (GNU libtool) 2.2.6 #
# Please DO NOT delete this file!
# It is necessary for linking the library.
# Name of the PIC object.
pic_object='.libs/foo.o' //动态库与代码无关的.o
# Name of the non-PIC object.
non_pic_object='foo.o' //静态库的.o
链接:
静态库a23$ libtool --mode=link gcc -g -O -o libhello.la foo.lo hello.lo / -rpath /usr/local/lib -lm
a23$ libtool --mode=link gcc -g -O -o libhello.la foo.o hello.o
*** Warning: Linking the shared library libhello.la against the non-libtool
*** objects foo.o hello.o is not portable!
相当于:ar cru .libs/libhello.a
ranlib .libs/libhello.a
creating libhello.la
(cd .libs && rm -f libhello.la && ln -s ../libhello.la libhello.la)
Another complication in building shared libraries is that we need to
Specify the path to the directory in which they (eventually) will be
installed (in this case, /usr/local/lib)1: 安装位置
相当于: ar cru .libs/libhello.a foo.o hello.o
ranlib .libs/libhello.a
creating libhello.la (cd .libs && rm -f libhello.la && ln -s ../libhello.la libhello.la)
动态库:
burger$ libtool --mode=link gcc -g -O -o libhello.la foo.lo hello.lo /
-rpath /usr/local/lib -lm
相当于:rm -fr .libs/libhello.a .libs/libhello.la
ld -Bshareable -o .libs/libhello.so.0.0 .libs/foo.o .libs/hello.o -lm
ar cru .libs/libhello.a foo.o hello.o
ranlib .libs/libhello.a
creating libhello.la
(cd .libs && rm -f libhello.la && ln -s ../libhello.la libhello.la)
burger$
Again, you may want to have a look at the ‘.la’ file in order
to see what Libtool stores in it. In particular, you will see that
Libtool uses this file to remember the destination directory for the
library (the argument to -rpath) as well as the dependency
on the math library (‘-lm’).
3.5 Installing libraries
Installing libraries on a non-libtool system is quite straightforward... just copy them into place:4
burger$ su Password: ********burger# cp libhello.a /usr/local/lib/libhello.a burger
burger# ranlib /usr/local/lib/libhello.a burger # Libtool installation is quite simple, as well. Just use the install or cp command that you normally would (see Install mode):
a23# libtool --mode=install cp libhello.la /usr/local/lib/libhello.la
cp libhello.la /usr/local/lib/libhello.la
cp .libs/libhello.a /usr/local/lib/libhello.a
ranlib /usr/local/lib/libhello.a
a23#
Note that the libtool library libhello.la is also installed, to help libtool with uninstallation (see Uninstall mode) and linking (see Linking executables) and to help programs with dlopening (see Dlopened modules).
Here is the shared library example:
动态库安装
install -c libhello.la /usr/local/lib/libhello.la
install -c .libs/libhello.a /usr/local/lib/libhello.a
ranlib /usr/local/lib/libhello.a
burger#
It is safe to specify the -s (strip symbols) flag if you use a BSD-compatible install program when installing libraries. Libtool will either ignore the -s flag, or will run a program that will strip only debugging and compiler symbols from the library.
Once the libraries have been put in place, there may be some additional configuration that you need to do before using them. First, you must make sure that where the library is installed actually agrees with the -rpath flag you used to build it.