⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libtool.texi

📁 GNU libtool 是一个通用库支持脚本
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
Now assume @file{libhello.la} had already been installed, and you wantto link a new program with it.  You could figure out where it lives byyourself, then run:@exampleburger$ @kbd{gcc -g -O -o test test.o -L/usr/local/lib -lhello}@end exampleHowever, unless @file{/usr/local/lib} is in the standard library searchpath, you won't be able to run @code{test}.  However, if you use libtoolto link the already-installed libtool library, it will do The RightThing (TM) for you:@exampleburger$ @kbd{libtool --mode=link gcc -g -O -o test \                test.o /usr/local/lib/libhello.la}gcc -g -O -o @value{objdir}/test test.o -Wl,--rpath-Wl,/usr/local/lib /usr/local/lib/libhello.a -lmcreating testburger$@end exampleNote that libtool added the necessary run-time path flag, as well as@samp{-lm}, the library libhello.la depended upon.  Nice, huh?Since libtool created a wrapper script, you should use libtool toinstall it and debug it too.  However, since the program does not dependon any uninstalled libtool library, it is probably usable even withoutthe wrapper script.  Libtool could probably be made smarter to avoid thecreation of the wrapper script in this case, but this is left as anexercise for the reader.@cindex wrapper scripts for programs@cindex program wrapper scriptsNotice that the executable, @code{hell}, was actually created in the@file{@value{objdir}} subdirectory.  Then, a wrapper script was createdin the current directory.On NetBSD 1.2, libtool encodes the installation directory of@file{libhello}, by using the @samp{-R/usr/local/lib} compiler flag.Then, the wrapper script guarantees that the executable finds thecorrect shared library (the one in @file{./@value{objdir}}) until it isproperly installed.Let's compare the two different programs:@exampleburger$ @kbd{time ./hell.old}Welcome to GNU Hell!** This is not GNU Hello.  There is no built-in mail reader. **        0.21 real         0.02 user         0.08 sysburger$ @kbd{time ./hell}Welcome to GNU Hell!** This is not GNU Hello.  There is no built-in mail reader. **        0.63 real         0.09 user         0.59 sysburger$@end exampleThe wrapper script takes significantly longer to execute, but at leastthe results are correct, even though the shared library hasn't beeninstalled yet.So, what about all the space savings that shared libraries are supposedto yield?@exampleburger$ @kbd{ls -l hell.old libhello.a}-rwxr-xr-x  1 gord  gord  15481 Nov 14 12:11 hell.old-rw-r--r--  1 gord  gord   4274 Nov 13 18:02 libhello.aburger$ @kbd{ls -l @value{objdir}/hell @value{objdir}/libhello.*}-rwxr-xr-x  1 gord  gord  11647 Nov 14 12:10 @value{objdir}/hell-rw-r--r--  1 gord  gord   4274 Nov 13 18:44 @value{objdir}/libhello.a-rwxr-xr-x  1 gord  gord  12205 Nov 13 18:44 @value{objdir}/libhello.so.0.0burger$@end exampleWell, that sucks.  Maybe I should just scrap this project and take upbasket weaving.Actually, it just proves an important point: shared libraries incuroverhead because of their (relative) complexity.  In this situation, theprice of being dynamic is eight kilobytes, and the payoff is about fourkilobytes.  So, having a shared @file{libhello} won't be an advantageuntil we link it against at least a few more programs.@node Debugging executables@section Debugging executablesIf @file{hell} was a complicated program, you would certainly want totest and debug it before installing it on your system.  In the abovesection, you saw how the libtool wrapper script makes it possible to runthe program directly, but unfortunately, this mechanism interferes withthe debugger:@exampleburger$ @kbd{gdb hell}GDB is free software and you are welcome to distribute copies of it under certain conditions; type "show copying" to see the conditions.There is no warranty for GDB; type "show warranty" for details.GDB 4.16 (i386-unknown-netbsd), (C) 1996 Free Software Foundation, Inc."hell": not in executable format: File format not recognized(gdb) @kbd{quit}burger$@end exampleSad.  It doesn't work because GDB doesn't know where the executablelives.  So, let's try again, by invoking GDB directly on the executable:@exampleburger$ @kbd{gdb @value{objdir}/hell}trick:/home/src/libtool/demo$ gdb .libs/hellGDB is free software and you are welcome to distribute copies of it under certain conditions; type "show copying" to see the conditions.There is no warranty for GDB; type "show warranty" for details.GDB 4.16 (i386-unknown-netbsd), (C) 1996 Free Software Foundation, Inc.(gdb) @kbd{break main}Breakpoint 1 at 0x8048547: file main.c, line 29.(gdb) @kbd{run}Starting program: /home/src/libtool/demo/.libs/hell/home/src/libtool/demo/.libs/hell: can't load library 'libhello.so.2'Program exited with code 020.(gdb) @kbd{quit}burger$@end exampleArgh.  Now GDB complains because it cannot find the shared library that@file{hell} is linked against.  So, we must use libtool in order toproperly set the library path and run the debugger.  Fortunately, we canforget all about the @file{@value{objdir}} directory, and just run it onthe executable wrapper (@pxref{Execute mode}):@exampleburger$ @kbd{libtool --mode=execute gdb hell}GDB is free software and you are welcome to distribute copies of it under certain conditions; type "show copying" to see the conditions.There is no warranty for GDB; type "show warranty" for details.GDB 4.16 (i386-unknown-netbsd), (C) 1996 Free Software Foundation, Inc.(gdb) @kbd{break main}Breakpoint 1 at 0x8048547: file main.c, line 29.(gdb) @kbd{run}Starting program: /home/src/libtool/demo/.libs/hellBreakpoint 1, main (argc=1, argv=0xbffffc40) at main.c:2929	  printf ("Welcome to GNU Hell!\n");(gdb) @kbd{quit}The program is running.  Quit anyway (and kill it)? (y or n) @kbd{y}burger$@end example@node Installing libraries@section Installing libraries@pindex stripInstalling libraries on a non-libtool system is quitestraightforward@dots{} just copy them into place:@footnote{Don'taccidentally strip the libraries, though, or they will be unusable.}@pindex su@exampleburger$ @kbd{su}Password: @kbd{********}burger# @kbd{cp libhello.a /usr/local/lib/libhello.a}burger#@end exampleOops, don't forget the @code{ranlib} command:@exampleburger# @kbd{ranlib /usr/local/lib/libhello.a}burger#@end example@pindex installLibtool installation is quite simple, as well.  Just use the@code{install} or @code{cp} command that you normally would(@pxref{Install mode}):@examplea23# @kbd{libtool --mode=install cp libhello.la /usr/local/lib/libhello.la}cp libhello.la /usr/local/lib/libhello.lacp @value{objdir}/libhello.a /usr/local/lib/libhello.aranlib /usr/local/lib/libhello.aa23#@end exampleNote that the libtool library @file{libhello.la} is also installed, tohelp libtool with uninstallation (@pxref{Uninstall mode}) and linking(@pxref{Linking executables}) and to help programs with dlopening(@pxref{Dlopened modules}).Here is the shared library example:@exampleburger# @kbd{libtool --mode=install install -c libhello.la \                /usr/local/lib/libhello.la}install -c @value{objdir}/libhello.so.0.0 /usr/local/lib/libhello.so.0.0install -c libhello.la /usr/local/lib/libhello.lainstall -c @value{objdir}/libhello.a /usr/local/lib/libhello.aranlib /usr/local/lib/libhello.aburger#@end example@cindex stripping libraries@cindex libraries, strippingIt is safe to specify the @samp{-s} (strip symbols) flag if you use aBSD-compatible install program when installing libraries.Libtool will either ignore the @samp{-s} flag, or will run a programthat will strip only debugging and compiler symbols from the library.Once the libraries have been put in place, there may be some additionalconfiguration that you need to do before using them.  First, you mustmake sure that where the library is installed actually agrees with the@samp{-rpath} flag you used to build it.@cindex postinstallation@cindex installation, finishing@cindex libraries, finishing installationThen, running @samp{libtool -n --mode=finish @var{libdir}} can give youfurther hints on what to do (@pxref{Finish mode}):@exampleburger# @kbd{libtool -n --mode=finish /usr/local/lib}PATH="$PATH:/sbin" ldconfig -m /usr/local/lib-----------------------------------------------------------------Libraries have been installed in:   /usr/local/libTo link against installed libraries in a given directory, LIBDIR,you must use the `-LLIBDIR' flag during linking. You will also need to do one of the following:   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable     during execution   - add LIBDIR to the `LD_RUN_PATH' environment variable     during linking   - use the `-RLIBDIR' linker flagSee any operating system documentation about shared libraries formore information, such as the ld and ld.so manual pages.-----------------------------------------------------------------burger#@end exampleAfter you have completed these steps, you can go on to begin using theinstalled libraries.  You may also install any executables that dependon libraries you created.@node Installing executables@section Installing executablesIf you used libtool to link any executables against uninstalled libtoollibraries (@pxref{Linking executables}), you need to use libtool toinstall the executables after the libraries have been installed(@pxref{Installing libraries}).So, for our Ultrix example, we would run:@examplea23# libtool install -c hell /usr/local/bin/hellinstall -c hell /usr/local/bin/hella23#@end exampleOn shared library systems, libtool just ignores the wrapper script andinstalls the correct binary:@exampleburger# libtool install -c hell /usr/local/bin/hellinstall -c @value{objdir}/hell /usr/local/bin/hellburger#@end example@node Static libraries@section Linking static libraries@cindex static linking@cindex convenience librariesWhy return to @code{ar} and @code{ranlib} silliness when you've had ataste of libtool?  Well, sometimes it is desirable to create a staticarchive that can never be shared.  The most frequent case is when youhave a set of object files that you use to build several differentprograms.  You can create a ``convenience library'' out of thoseobjects, and link programs with the library, instead of listing allobject files for every program.  This technique is often used toovercome GNU automake's lack of support for linking object files builtfrom sources in other directories, because it supports linking withlibraries from other directories.  This limitation applies to GNUautomake up to release 1.4; newer releases should support sources inother directories.If you just want to link this convenience library into programs, thenyou could just ignore libtool entirely, and use the old @code{ar} and@code{ranlib} commands (or the corresponding GNU automake@samp{_LIBRARIES} rules).  You can even install a convenience library(but you probably don't want to) using libtool:@exampleburger$ @kbd{libtool --mode=install ./install-sh -c libhello.a \                /local/lib/libhello.a}./install-sh -c libhello.a /local/lib/libhello.aranlib /local/lib/libhello.aburger$@end exampleUsing libtool for static library installation protects your library frombeing accidentally stripped (if the installer used the @samp{-s} flag),as well as automatically running the correct @code{ranlib} command.But libtool libraries are more than just collections of object files:they can also carry library dependency information, which old archivesdo not.  If you want to create a libtool static convenience library, youcan omit the @samp{-rpath} flag and use @samp{-static} to indicate thatyou're only interested in a static library.  When you link a programwith such a library, libtool will actually link all object files anddependency libraries into the program.If you omit both @samp{-rpath} and @samp{-static}, libtool will create aconvenience library that can be used to create other libtoollibraries, even shared ones.  Just like in the static case, the librarybehaves as an alias to a set of object files and dependency libraries,but in this case the object files are suitable for inclusion in sharedlibraries.  But be careful not to link a single convenience library,directly or indirectly, into a single program or library, otherwise youmay get errors about symbol redefinitions.When GNU automake is used, you should use @code{noinst_LTLIBRARIES}instead of @code{lib_LTLIBRARIES} for convenience libraries, so thatthe @samp{-rpath} option is not passed when they are linked.As a rule of thumb, link a libtool convenience library into at most onelibtool library, and never into a program, and link libtool staticconvenience libraries only into programs, and only if you need to carrylibrary dependency information to the user of the static conveniencelibrary.@cindex standalone binariesAnother common situation where static linking is desirable is increating a standalone binary.  Use libtool to do the linking and add the@samp{-all-static} flag.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -