TExcelTAD
Home › Meson, Autotools & xmake › Shared Libraries with libtool

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:

  1. LT_INIT in configure.ac (plus AM_PROG_AR for the archiver), and AC_CONFIG_MACRO_DIRS([m4]) so libtoolize copies its macros into m4/;
  2. lib_LTLIBRARIES = libgreet.la: the library is named with a .la extension;
  3. -version-info current:revision:age in libgreet_la_LDFLAGS for 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
./hello
libtoolize: 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 '^#|^
  

 libgreet.la
echo "--- hello"
file hello | cut -d, -f1
file .libs/hello | cut -d, -f1
./hello && echo "the wrapper ran .libs/hello with the uninstalled library"
echo "--- install"
make -j1 -s install DESTDIR=$PWD/stage 2>&1 | grep 'libtool: warning'
find stage -name 'libgreet*' | sort
readelf -d stage/usr/local/lib/libgreet.so.2.1.0 | grep SONAME
--- libgreet.la
dlname='libgreet.so.2'
library_names='libgreet.so.2.1.0 libgreet.so.2 libgreet.so'
old_library='libgreet.a'
inherited_linker_flags=''
dependency_libs=''
weak_library_names=''
current=3
age=1
revision=0
installed=no
shouldnotlink=no
dlopen=''
dlpreopen=''
libdir='/usr/local/lib'
--- hello
hello: Bourne-Again shell script
.libs/hello: ELF 64-bit LSB pie executable
the wrapper ran .libs/hello with the uninstalled library
--- install
libtool: warning: remember to run 'libtool --finish /usr/local/lib'
libtool: warning: 'libgreet.la' has not been installed in '/usr/local/lib'
stage/usr/local/lib/libgreet.a
stage/usr/local/lib/libgreet.la
stage/usr/local/lib/libgreet.so
stage/usr/local/lib/libgreet.so.2
stage/usr/local/lib/libgreet.so.2.1.0
 0x000000000000000e (SONAME)             Library soname: [libgreet.so.2]
  • The .la file records the library names, the old-style static archive, the dependency_libs (so linking against libgreet.la also links its dependencies, even for static linking) and the install libdir. It matters to other libtool builds; for everyone else, ship the .pc file instead. Most distributions delete installed .la files, because the recorded dependencies cause over-linking.
  • The wrapper script sets up the environment so the uninstalled program finds the uninstalled .libs/libgreet.so. To debug it, run libtool --mode=execute gdb ./hello, not gdb ./hello (which would debug the shell).
  • The two warnings appear because of DESTDIR: --finish would run ldconfig on the real /usr/local/lib, and the program was relinked/installed against a library that is not (yet) in /usr/local/lib. Both are safe to ignore when staging a package.

-version-info is not the package version

-version-info current:revision:age describes the library's interface, and the rules for updating it are mechanical. For each release:

  1. If the source changed at all, increment revision.
  2. If any interface was added, removed or changed, increment current and set revision to 0.
  3. If interfaces were added, increment age.
  4. If interfaces were removed or changed (incompatibly), set age to 0.

On Linux libtool turns it into libNAME.so.(current − age).(age).(revision), with soname libNAME.so.(current − age). That is why 3:0:1 gave libgreet.so.2.1.0. The model below applies the rules to a sequence of releases; it prints the soname that consumers' programs record, which only changes when the ABI breaks:

js · try it yourself
function nextVersion([cur, rev, age], change) {
  if (change === 'bugfix') return [cur, rev + 1, age];             // rule 1
  if (change === 'added') return [cur + 1, 0, age + 1];            // rules 1-3
  if (change === 'removed' || change === 'changed') return [cur + 1, 0, 0];   // rules 1, 2, 4
  throw new Error(change);
}
const linuxNames = ([c, r, a]) => ({ file: `libgreet.so.${c - a}.${a}.${r}`, soname: `libgreet.so.${c - a}` });

let v = [0, 0, 0];
const releases = [
  ['1.0.0', null], ['1.0.1', 'bugfix'], ['1.1.0', 'added'], ['1.1.1', 'bugfix'],
  ['1.2.0', 'added'], ['2.0.0', 'removed'], ['2.0.1', 'bugfix'],
];
for (const [pkg, change] of releases) {
  if (change) v = nextVersion(v, change);
  const { file, soname } = linuxNames(v);
  console.log(`${pkg.padEnd(6)} ${(change ?? 'first').padEnd(8)} -version-info ${v.join(':').padEnd(6)} ${file.padEnd(21)} soname ${soname}`);
}
1.0.0  first    -version-info 0:0:0  libgreet.so.0.0.0     soname libgreet.so.0
1.0.1  bugfix   -version-info 0:1:0  libgreet.so.0.0.1     soname libgreet.so.0
1.1.0  added    -version-info 1:0:1  libgreet.so.0.1.0     soname libgreet.so.0
1.1.1  bugfix   -version-info 1:1:1  libgreet.so.0.1.1     soname libgreet.so.0
1.2.0  added    -version-info 2:0:2  libgreet.so.0.2.0     soname libgreet.so.0
2.0.0  removed  -version-info 3:0:0  libgreet.so.3.0.0     soname libgreet.so.3
2.0.1  bugfix   -version-info 3:1:0  libgreet.so.3.0.1     soname libgreet.so.3

Tip: never derive -version-info from the package version (-version-info 1:2:0 for release 1.2.0 is a classic mistake). Projects that want the soname to follow the release number use -release 1.2 instead, which makes libgreet-1.2.so and declares "no compatibility between releases".

Test yourself

In an uninstalled libtool build, what is ./hello?

  1. The real ELF program
  2. A wrapper shell script that runs .libs/hello with the uninstalled shared library
  3. A symlink to .libs/hello
  4. A .la file

A library at -version-info 5:2:3 adds a new function and changes nothing else. What is the next -version-info?

  1. 5:3:3
  2. 6:0:4
  3. 6:0:0
  4. 5:2:4

Why do many Linux distributions delete installed .la files?

  1. They are large
  2. Their recorded dependency_libs make dependent builds link more libraries than needed; pkg-config files serve the same purpose
  3. They contain the source code
  4. They break ldconfig