Next: , Previous: , Up: Building Programs and Libraries   [Contents][Index]


7.6 Special handling for LIBOBJS and ALLOCA

The $(LIBOBJS) and $(ALLOCA) variables list object files that should be compiled into the project to provide an implementation for functions that are missing or broken on the host system. They are substituted by configure.

These variables are defined by Autoconf macros such as AC_LIBOBJ, AC_REPLACE_FUNCS (see Generic Function Checks in The Autoconf Manual), or AC_FUNC_ALLOCA (see Particular Function Checks in The Autoconf Manual). Many other Autoconf macros call AC_LIBOBJ or AC_REPLACE_FUNCS to populate $(LIBOBJS).

Using these variables is very similar to doing conditional compilation using AC_SUBST variables, as described in Conditional compilation of sources. That is, when building a program, $(LIBOBJS) and $(ALLOCA) should be added to the associated ‘*_LDADD’ variable, or to the ‘*_LIBADD’ variable when building a library. However there is no need to list the corresponding sources in EXTRA_*_SOURCES nor to define *_DEPENDENCIES. Automake automatically adds $(LIBOBJS) and $(ALLOCA) to the dependencies, and it will discover the list of corresponding source files automatically (by tracing the invocations of the AC_LIBSOURCE Autoconf macros).

These variables are usually used to build a portability library that is linked with all the programs of the project. We now review a sample setup. First, configure.ac contains some checks that affect either LIBOBJS or ALLOCA.

# configure.ac
…
AC_CONFIG_LIBOBJ_DIR([lib])
…
AC_FUNC_MALLOC             dnl May add malloc.$(OBJEXT) to LIBOBJS
AC_FUNC_MEMCMP             dnl May add memcmp.$(OBJEXT) to LIBOBJS
AC_REPLACE_FUNCS([strdup]) dnl May add strdup.$(OBJEXT) to LIBOBJS
AC_FUNC_ALLOCA             dnl May add alloca.$(OBJEXT) to ALLOCA
…
AC_CONFIG_FILES([
  lib/Makefile
  src/Makefile
])
AC_OUTPUT

The AC_CONFIG_LIBOBJ_DIR tells Autoconf that the source files of these object files are to be found in the lib/ directory. Automake does not yet use this information; it knows the source files are expected to be in the directory where the $(LIBOBJS) and $(ALLOCA) variables are used.

The lib/ directory should therefore contain malloc.c, memcmp.c, strdup.c, alloca.c. Here is its Makefile.am:

# lib/Makefile.am

noinst_LIBRARIES = libcompat.a
libcompat_a_SOURCES =
libcompat_a_LIBADD = $(LIBOBJS) $(ALLOCA)

Nothing else is required. The library can have any name, of course, and anyway it is not going to be installed: it just holds the replacement versions of the missing or broken functions so we can later link them in. In many projects also include extra functions, specific to the project, in that library: they are simply added on the _SOURCES line.

Finally here is how this library could be used from the src/ directory.

# src/Makefile.am

# Link all programs in this directory with libcompat.a
LDADD = ../lib/libcompat.a

bin_PROGRAMS = tool1 tool2 …
tool1_SOURCES = …
tool2_SOURCES = …

Please note it would be wrong to use the $(LIBOBJS) or $(ALLOCA) in src/Makefile.am, because these variables contains unprefixed object names, and for instance malloc.$(OBJEXT) is not buildable in the src/ directory. (Actually if you try using $(LIBOBJS) in src/, Automake will require a copy of malloc.c, memcmp.c, strdup.c, alloca.c in src/ too.)

Because $(LIBOBJS) and $(ALLOCA) contain object filenames that end with .$(OBJEXT), they are not suitable for Libtool libraries (where the expected object extension is .lo): LTLIBOBJS and LTALLOCA should be used instead.

LTLIBOBJS is defined automatically by Autoconf and should not be defined by hand (as in the past), however at the time of writing LTALLOCA still needs to be defined from ALLOCA manually. See AC_LIBOBJ vs. LIBOBJS in The Autoconf Manual.


Next: Variables used when building a program, Previous: Default _SOURCES, Up: Building Programs and Libraries   [Contents][Index]