Shared Libraries with libtool
Building a shared library is different on every platform: -fPIC and -shared and sonames on Linux, -dynamiclib and install names on macOS, DLLs plus import libraries on Windows, and in the 1990s a dozen commercial Unixes with their own rules. GNU libtool hides all of that behind one interface. Automake uses it whenever you write LTLIBRARIES instead of LIBRARIES.
Turning libtool on
Three changes, compared with a plain automake project:
LT_INITinconfigure.ac(plusAM_PROG_ARfor the archiver), andAC_CONFIG_MACRO_DIRS([m4])solibtoolizecopies its macros intom4/;lib_LTLIBRARIES = libgreet.la: the library is named with a.laextension;-version-info current:revision:ageinlibgreet_la_LDFLAGSfor the library's ABI version.
bash · try it yourself
mkdir -p greet/src greet/m4 && cd greet
cat > configure.ac <<'AC'
AC_INIT([greet], [1.2.0])
AC_CONFIG_SRCDIR([src/greet.c])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIRS([m4])
AM_INIT_AUTOMAKE([foreign -Wall -Werror subdir-objects])
AM_SILENT_RULES([yes])
AM_PROG_AR
LT_INIT
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
AC
cat > Makefile.am <<'AM'
ACLOCAL_AMFLAGS = -I m4
lib_LTLIBRARIES = libgreet.la
libgreet_la_SOURCES = src/greet.c
libgreet_la_LDFLAGS = -version-info 3:0:1
include_HEADERS = src/greet.h
bin_PROGRAMS = hello
hello_SOURCES = src/main.c
hello_LDADD = libgreet.la
AM
echo 'const char *greet(void);' > src/greet.h
echo 'const char *greet(void) { return "hello from libtool"; }' > src/greet.c
echo '#include <stdio.h>
#include "greet.h"
int main(void) { puts(greet()); return 0; }' > src/main.c
autoreconf -fi 2>&1 | grep libtoolize
./configure --quiet
make -j1
echo "--- the build tree"
ls .libs src/*.lo
./hellolibtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, 'build-aux'.
libtoolize: copying file 'build-aux/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4'.
libtoolize: copying file 'm4/libtool.m4'
libtoolize: copying file 'm4/ltoptions.m4'
libtoolize: copying file 'm4/ltsugar.m4'
libtoolize: copying file 'm4/ltversion.m4'
libtoolize: copying file 'm4/lt~obsolete.m4'
CC src/main.o
CC src/greet.lo
CCLD libgreet.la
CCLD hello
--- the build tree
src/greet.lo
.libs:
hello
libgreet.a
libgreet.la
libgreet.lai
libgreet.so
libgreet.so.2
libgreet.so.2.1.0
hello from libtool
Everything libtool builds goes through a libtool script that configure generated for this platform, and its outputs come in pairs:
| File | What it is |
|---|---|
src/greet.lo |
a text file pointing at two objects: src/.libs/greet.o (PIC, for the shared library) and src/greet.o (non-PIC, for the static one) |
libgreet.la |
a text file describing the library (below) |
.libs/libgreet.so.2.1.0 + symlinks |
the real shared library |
.libs/libgreet.lai |
the .la file as it will look once installed (installed=yes); make install copies it as libgreet.la |
.libs/libgreet.a |
the static library (LT_INIT([disable-static]) or ./configure --disable-static skips it) |
hello |
a wrapper shell script, not the program |
.libs/hello |
the real program |
The .la file and the wrapper script
bash · try it yourself
mkdir -p greet/src greet/m4 && cd greet
cat > configure.ac <<'AC'
AC_INIT([greet], [1.2.0])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIRS([m4])
AM_INIT_AUTOMAKE([foreign subdir-objects])
AM_PROG_AR
LT_INIT
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
AC
cat > Makefile.am <<'AM'
ACLOCAL_AMFLAGS = -I m4
lib_LTLIBRARIES = libgreet.la
libgreet_la_SOURCES = src/greet.c
libgreet_la_LDFLAGS = -version-info 3:0:1
bin_PROGRAMS = hello
hello_SOURCES = src/main.c
hello_LDADD = libgreet.la
AM
echo 'const char *greet(void) { return "hi"; }' > src/greet.c
echo 'const char *greet(void); int main(void) { return greet()[0] != 0x68; }' > src/main.c
autoreconf -fi > /dev/null 2>&1
./configure --quiet --prefix=/usr/local
make -j1 -s > /dev/null 2>&1
echo "--- libgreet.la"
grep -vE '^#|^