automake: A Library

1 
1 8.2 Building a library
1 ======================
1 
1 Building a library is much like building a program.  In this case, the
1 name of the primary is ‘LIBRARIES’.  Libraries can be installed in
1 ‘libdir’ or ‘pkglibdir’.
1 
1    ⇒A Shared Library, for information on how to build shared
1 libraries using libtool and the ‘LTLIBRARIES’ primary.
1 
1    Each ‘_LIBRARIES’ variable is a list of the libraries to be built.
1 For instance, to create a library named ‘libcpio.a’, but not install it,
1 you would write:
1 
1      noinst_LIBRARIES = libcpio.a
1      libcpio_a_SOURCES = ...
1 
1    The sources that go into a library are determined exactly as they are
1 for programs, via the ‘_SOURCES’ variables.  Note that the library name
1 is canonicalized (⇒Canonicalization), so the ‘_SOURCES’ variable
1 corresponding to ‘libcpio.a’ is ‘libcpio_a_SOURCES’, not
1 ‘libcpio.a_SOURCES’.
1 
1    Extra objects can be added to a library using the ‘LIBRARY_LIBADD’
1 variable.  This should be used for objects determined by ‘configure’.
1 Again from ‘cpio’:
1 
1      libcpio_a_LIBADD = $(LIBOBJS) $(ALLOCA)
1 
1    In addition, sources for extra objects that will not exist until
11 configure-time must be added to the ‘BUILT_SOURCES’ variable (⇒
 Sources).
1 
1    Building a static library is done by compiling all object files, then
1 by invoking ‘$(AR) $(ARFLAGS)’ followed by the name of the library and
1 the list of objects, and finally by calling ‘$(RANLIB)’ on that library.
1 You should call ‘AC_PROG_RANLIB’ from your ‘configure.ac’ to define
1 ‘RANLIB’ (Automake will complain otherwise).  You should also call
1 ‘AM_PROG_AR’ to define ‘AR’, in order to support unusual archivers such
1 as Microsoft lib.  ‘ARFLAGS’ will default to ‘cru’; you can override
1 this variable by setting it in your ‘Makefile.am’ or by ‘AC_SUBST’ing it
1 from your ‘configure.ac’.  You can override the ‘AR’ variable by
11 defining a per-library ‘maude_AR’ variable (⇒Program and Library
 Variables).
1 
1    Be careful when selecting library components conditionally.  Because
1 building an empty library is not portable, you should ensure that any
1 library always contains at least one object.
1 
1    To use a static library when building a program, add it to ‘LDADD’
1 for this program.  In the following example, the program ‘cpio’ is
1 statically linked with the library ‘libcpio.a’.
1 
1      noinst_LIBRARIES = libcpio.a
1      libcpio_a_SOURCES = ...
1 
1      bin_PROGRAMS = cpio
1      cpio_SOURCES = cpio.c ...
1      cpio_LDADD = libcpio.a
1