5.3 Using Automake with libtool

Libtool library support is implemented under the ‘LTLIBRARIES’ primary.

Here are some samples from the Automake Makefile.am in the libtool distribution’s tests/demo.at.

First, to link a program against a libtool library, just use the ‘program_LDADD6 variable:

bin_PROGRAMS = hell hell_static

# Build hell from main.c and libhello.la
hell_SOURCES = main.c
hell_LDADD = libhello.la

# Create a statically linked version of hell.
hell_static_SOURCES = main.c
hell_static_LDADD = libhello.la
hell_static_LDFLAGS = -static

You may use the ‘program_LDFLAGS’ variable to stuff in any flags you want to pass to libtool while linking program (such as -static to avoid linking uninstalled shared libtool libraries).

Building a libtool library is almost as trivial… note the use of ‘libhello_la_LDFLAGS’ to pass the -version-info (see Library interface versions) option to libtool:

# Build a libtool library, libhello.la for installation in libdir.
lib_LTLIBRARIES = libhello.la
libhello_la_SOURCES = hello.c foo.c
libhello_la_LDFLAGS = -version-info 3:12:1

The -rpath option is passed automatically by Automake (except for libraries listed as noinst_LTLIBRARIES), so you should not specify it.

See The Automake Manual in The Automake Manual, for more information.

When building libtool archives which depend on built sources (for example a generated header file), you may find it necessary to manually record these dependencies. Because libtool archives generate object file names manually recording these dependencies is not as straightforward as the examples in Automake’s manual describe. This affects header files in particular, because simply listing them as ‘nodist_libfoo_la_SOURCES’ will not cause Automake to establish a dependent relationship for the object files of libfoo.la. A useful trick (although somewhat imprecise) is to manually record built sources used by a libtool archive as dependencies of all the objects for that library as shown below (as opposed to a particular object file):

# Build a libtool library, libhello.la which depends on a generated header.
hello.h:
	echo '#define HELLO_MESSAGE  "Hello, World!"' > $@
BUILT_SOURCES = hello.h
CLEANFILES = hello.h
nodist_libhello_la_SOURCES = hello.h
libhello_la_SOURCES = hello.c foo.h foo.c bar.h bar.c
# Manually record hello.h as a prerequisite for all objects in libhello.la
$(libhello_la_OBJECTS): hello.h

See The Automake Manual in The Automake Manual, for more information.


Footnotes

(6)

Since GNU Automake 1.5, the flags -dlopen or -dlpreopen (see Link mode) can be employed with the ‘program_LDADD’ variable. Unfortunately, older releases didn’t accept these flags, so if you are stuck with an ancient Automake, we recommend quoting the flag itself, and setting ‘program_DEPENDENCIES’ too:

program_LDADD = "-dlopen" libfoo.la
program_DEPENDENCIES = libfoo.la