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

📄 autobook_92.html

📁 Autoconf使用手册
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<TT>`error.c'</TT>.  The problem with this approach is that differentvendors will provide slightly different declarations (with or without<CODE>const</CODE> for instance), so compilation will fail on thosearchitectures which <EM>do</EM> provide a declaration in the systemheaders that is different from the declaration you have guessed.</P><P>For the benefit of architectures which do not have an implementation ofthe <CODE>basename()</CODE> function, a fallback implementation is provided inthe <TT>`replace'</TT> subdirectory.  The file <TT>`basename.c'</TT> follows:</P><P><TABLE width=100%><tr><td>&nbsp;</td><td class=example bgcolor=#6688aa><br><pre></pre></td></tr></table></P><P>For brevity, the implementation does not use any <CODE>const</CODE>declarations which would be good style for a real project, but wouldneed to be checked at configure time in case the end user needs tocompile the package with a K&#38;R compiler.</P><P>The use of <CODE>strrchr()</CODE> is noteworthy.  Sometimes it is declared in<TT>`string.h'</TT>, otherwise it might be declared in <TT>`strings.h'</TT>.<FONT SIZE="-1">BSD</FONT> based Unices, on the other hand, do not have this function atall, but provide an equivalent function, <CODE>rindex()</CODE>.  Thepreprocessor code at the start of the file is designed to cope with allof these eventualities.  The last block of preprocessor code assumesthat if <CODE>strrchr</CODE> is already defined that it holds a working macro,and does not redefine it.</P><P><TT>`Makefile.am'</TT> contains:</P><P><TABLE width=100%><tr><td>&nbsp;</td><td class=example bgcolor=#6688aa><br><pre></pre></td></tr></table></P><P>Once again, the <SAMP>`noinst_LTLIBRARIES'</SAMP> macro names the conveniencelibrary,<BR> <TT>`libreplace.la'</TT>.  By default there are no sources, sincewe expect to have a system definition of <CODE>basename()</CODE>.  AdditionalLibtool objects which should be added to the library based on tests atconfigure time are handled by the <SAMP>`LIBADD'</SAMP> macro.  <SAMP>`LTLIBOBJS'</SAMP>will contain <TT>`basename.lo'</TT> if the system does not provide<CODE>basename</CODE>, and will be empty otherwise.  Illustrating anotherfeature of convenience libraries: on many architectures,<TT>`libreplace.la'</TT> will contain no objects.</P><P>Back in the toplevel project directory, all of the preceding objects arecombined by another <TT>`Makefile.am'</TT>:</P><P><TABLE width=100%><tr><td>&nbsp;</td><td class=example bgcolor=#6688aa><br><pre></pre></td></tr></table></P><P>The initial <SAMP>`SUBDIRS'</SAMP> macro is necessary to ensure that thelibraries in the subdirectories are built before the final library andexecutable in this directory.</P><P>Notice that I have not listed <TT>`error.h'</TT> in<SAMP>`liberror_la_SOURCES'</SAMP> this time, since <TT>`liberror.la'</TT> is aninstalled library, and <TT>`error.h'</TT> defines the public interface tothat library.  Since the <TT>`liberror.la'</TT> Libtool library isinstalled, I have used the <SAMP>`-version-info'</SAMP> option, and I havealso used <SAMP>`-no-undefined'</SAMP> so that the project will compile onarchitectures which require all library symbols to be defined at linktime -- the reason <CODE>program_name</CODE> is maintained in<TT>`liberror'</TT> rather than <TT>`main.c'</TT> is so that the library doesnot have a runtime dependency on the executable which links it.</P><P>The key to this example is that by linking the <TT>`libreplace.la'</TT> and<TT>`library.la'</TT> convenience libraries into <TT>`liberror.la'</TT>, all ofthe objects in both convenience libraries are compiled into the singleinstalled library, <TT>`liberror.la'</TT>.  Additionally, all of theinter-library dependencies of the convenience libraries (<SAMP>`-lm'</SAMP>,from <TT>`library.la'</TT>) are propagated to <TT>`liberror.la'</TT>.</P><P><BLOCKQUOTE>A common difficulty people experience with Automake is knowing when touse a <SAMP>`LIBADD'</SAMP> primary versus a <SAMP>`LDADD'</SAMP> primary.  A usefulmnemonic is: <STRONG><SAMP>`LIBADD'</SAMP> is for ADDitional LIBrary objects.<SAMP>`LDADD'</SAMP> is for ADDitional linker (LD) objects.</STRONG></BLOCKQUOTE><P>The executable, <TT>`convenience'</TT>, is built from <TT>`main.c'</TT>, andrequires only <TT>`liberror.la'</TT>.  All of the other implicitdependencies are encoded within <TT>`liberror.la'</TT>.  Here is<TT>`main.c'</TT>:</P><P><TABLE width=100%><tr><td>&nbsp;</td><td class=example bgcolor=#6688aa><br><pre></pre></td></tr></table></P><P>The only file that remains before you can compile the example is<TT>`configure.in'</TT>:</P><P><TABLE width=100%><tr><td>&nbsp;</td><td class=example bgcolor=#6688aa><br><pre></pre></td></tr></table></P><P>There are checks for all of the features used by the sources in theproject: <TT>`math.h'</TT> and either <TT>`string.h'</TT> or <TT>`strings.h'</TT>;the existence of <CODE>strrchr</CODE> (<EM>after</EM> the tests for stringheaders); adding <TT>`basename.o'</TT> to <SAMP>`LIBOBJS'</SAMP> if there is nosystem implementation; and the shell code to set <SAMP>`LTLIBOBJS'</SAMP>.</P><P>With all the files in place, you can now bootstrap the project:</P><P><TABLE><tr><td>&nbsp;</td><td class=smallexample><FONT SIZE=-1><pre>$ ls -R.:Makefile.am  configure.in  error.c  error.h  lib  main.c  replacelib:Makefile.am  source.c  source.hreplace:Makefile.am  basename.c$ aclocal$ autoheader$ automake --add-missing --copyautomake: configure.in: installing ./install-shautomake: configure.in: installing ./mkinstalldirsautomake: configure.in: installing ./missingconfigure.in: 7: required file ./ltconfig not found$ autoconf$ ls -R.:Makefile.am   config.h.in   error.c     ltconfig   mkinstalldirsMakefile.in   config.sub    error.h     ltmain.sh  replaceaclocal.m4    configure     install-sh  main.cconfig.guess  configure.in  lib         missinglib:Makefile.am  Makefile.in  source.c  source.hreplace:Makefile.am  Makefile.in  basename.c</FONT></pre></td></tr></table></P><P>With these files in place, the package can now be configured:</P><P><TABLE><tr><td>&nbsp;</td><td class=smallexample><FONT SIZE=-1><pre>$ ./configure...checking how to run the C preprocessor... gcc -Echecking for math.h... yeschecking for string.h... yeschecking for strrchr... yeschecking for basename... yesupdating cache ./config.cachecreating ./config.statuscreating replace/Makefilecreating lib/Makefilecreating Makefilecreating config.h</FONT></pre></td></tr></table></P><P>Notice that my host has an implementation of <CODE>basename()</CODE>.</P><P>Here are the highlights of the compilation itself:</P><P><TABLE><tr><td>&nbsp;</td><td class=smallexample><FONT SIZE=-1><pre>$ makeMaking all in replacemake[1]: Entering directory /tmp/replace/bin/sh ../libtool --mode=link gcc  -g -O2  -o libreplace.larm -fr .libs/libreplace.la .libs/libreplace.* .libs/libreplace.*ar cru .libs/libreplace.alranlib .libs/libreplace.alcreating libreplace.la(cd .libs &#38;&#38; rm -f libreplace.la &#38;&#38; ln -s ../libreplace.la \libreplace.la)make[1]: Leaving directory /tmp/replace</FONT></pre></td></tr></table></P><P>Here the build descends into the <TT>`replace'</TT> subdirectory and creates<TT>`libreplace.la'</TT>, which is empty on my host since I don't need animplementation of <CODE>basename()</CODE>:</P><P><TABLE><tr><td>&nbsp;</td><td class=smallexample><FONT SIZE=-1><pre>Making all in libmake[1]: Entering directory /tmp/lib/bin/sh ../libtool --mode=compile gcc -DHAVE_CONFIG_H  -I. -I. \-g -O2 -c source.crm -f .libs/source.logcc -DHAVE_CONFIG_H -I. -I. -g -O2 -c -fPIC -DPIC source.c \-o .libs/source.logcc -DHAVE_CONFIG_H -I. -I. -g -O2 -c source.c \-o source.o &#62;/dev/null 2&#62;&#38;1mv -f .libs/source.lo source.lo/bin/sh ../libtool --mode=link gcc  -g -O2  -o library.la source.lo -lmrm -fr .libs/library.la .libs/library.* .libs/library.*ar cru .libs/library.al source.loranlib .libs/library.alcreating library.la(cd .libs &#38;&#38; rm -f library.la &#38;&#38; ln -s ../library.la library.la)make[1]: Leaving directory /tmp/lib</FONT></pre></td></tr></table></P><P>Next, the build enters the <TT>`lib'</TT> subdirectory to build<TT>`library.la'</TT>.  The <TT>`configure'</TT> preprocessor macros are passedon the command line, since no <TT>`config.h'</TT> was created by<CODE>AC_CONFIG_HEADER</CODE>:</P><P>Here, <TT>`main.c'</TT> is compiled (not to a Libtool object, since it isnot compiled using <CODE>libtool</CODE>), and linked with the<TT>`liberror.la'</TT> Libtool library:</P><P><TABLE><tr><td>&nbsp;</td><td class=smallexample><FONT SIZE=-1><pre>gcc -DHAVE_CONFIG_H -I. -I.  -I./lib  -g -O2 -c main.c/bin/sh ./libtool --mode=link gcc  -g -O2  -o convenience  main.o \liberror.lagcc -g -O2 -o .libs/convenience main.o ./.libs/liberror.so -lm \-Wl,--rpath -Wl,/usr/local/libcreating conveniencemake[1]: Leaving directory /tmp/convenience</FONT></pre></td></tr></table></P><P><CODE>libtool</CODE> calls <CODE>gcc</CODE> to link the <CODE>convenience</CODE>executable from <TT>`main.o'</TT> and the shared library component of<TT>`liberror.la'</TT>.  <CODE>libtool</CODE> also links with <SAMP>`-lm'</SAMP>, thepropagated inter-library dependency of the <TT>`library.la'</TT> conveniencelibrary.  Since <TT>`libreplace.la'</TT> and <TT>`library.la'</TT> wereconvenience libraries, their objects are already present in<TT>`liberror.la'</TT>, so they are not listed again in the final link line-- the whole point of convenience archives.</P><P>This just shows that it all works:</P><P><TABLE><tr><td>&nbsp;</td><td class=smallexample><FONT SIZE=-1><pre>$ lsMakefile      config.h       configure.in  install-sh   main.cMakefile.am   config.h.in    convenience   lib          main.oMakefile.in   config.log     error.c       liberror.la  missingaclocal.m4    config.status  error.h       libtool      mkinstalldirsconfig.cache  config.sub     error.lo      ltconfig     replaceconfig.guess  configure      error.o       ltmain.sh$ libtool --mode=execute ldd convenience        liberror.so.0 =&#62; /tmp/.libs/liberror.so.0 (0x40014000)        libm.so.6 =&#62; /lib/libm.so.6 (0x4001c000)        libc.so.6 =&#62; /lib/libc.so.6 (0x40039000)        /lib/ld-linux.so.2 =&#62; /lib/ld-linux.so.2 (0x40000000)$ ./conveniencecos (0) =&#62; 1lt-convenience: ERROR: This program does nothing!</FONT></pre></td></tr></table></P><P>Notice that you are running the uninstalled executable, which is inactual fact a wrapper script, See section <A HREF="autobook_79.html#SEC79">10.5 Executing Uninstalled Binaries</A>.That is why you need to use <CODE>libtool</CODE> to run <CODE>ldd</CODE> onthe real executable.  The uninstalled executable called by the wrapperscript is called <CODE>lt-convenience</CODE>, hence the output from<CODE>basename()</CODE>.</P><P>Finally, you can see from the output of <CODE>ldd</CODE>, that<CODE>convenience</CODE> really isn't linked against either<TT>`library.la'</TT> and <TT>`libreplace.la'</TT>.</P><P><A NAME="A Large GNU Autotools Project"></A></TR></TABLE><BR>  <FONT SIZE="-1">This document was generatedby <I>Gary V. Vaughan</I> on <I>September, 12  2004</I>using <A HREF="http://www.mathematik.uni-kl.de/~obachman/Texi2html"><I>texi2html</I></A></BODY></HTML>

⌨️ 快捷键说明

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