Module 1 · Lesson 34 of 103

Building from source and shared libraries

After this lesson you can build and install a C program from source with configure, make and make install, and create a shared library's three names.

  • Beginner to advanced
  • 6 min read
  • 11 min lab
What you will need

Ubuntu 24.04 (WSL2, VM or container); a Raspberry Pi for the embedded notes

Your place in the module
By the end you can 2 objectives
  • Build and install a C program from source with configure, make and make install, controlling the location with --prefix and DESTDIR.
  • Distinguish the real name, soname and linker name of a shared library and create all three with gcc and symlinks.
Video coming soonBuilding from source and shared libraries
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Package managers and their families
  • A package bundles compiled files (or source) with metadata: name, version, dependencies, maintainer scripts.
  • apt and dnf resolve dependencies and talk to repositories; dpkg and rpm install and remove individual package files.
  • A .deb is an ar archive of three members; a .rpm is a metadata header followed by a cpio payload.
01

Building from source: configure, make, make install

4 min read

Packages are the default, but not every program is packaged for your board, and sometimes you must patch or cross-compile. This lesson answers how configure, make and make install build software and what the three names of a shared library are.

Building from source is the fallback when a program is not packaged for your distribution, when you need a version or a feature the package does not offer, when you must patch the code, or when you are cross-compiling for a board. The dominant build system on older and low-level projects is GNU Autotools, whose entry point is a configure script. Autotools generates that script and the templates around it, but as a user you only run three commands.

1configure

./configure probes your system: which compiler exists, which libraries and headers are present, which features can be enabled. It then writes a Makefile tuned to what it found. Options control the result. --prefix sets the installation root and defaults to /usr/local, which is why a source build lands there. --enable-NAME and --disable-NAME switch optional features on and off, and --with-NAME and --without-NAME select or reject external libraries.

bash
./configure --prefix=/opt/yourapp --enable-shared --disable-static
The configure, make, make install build flow
learner@embeddex:~/lab$ # Probe the system and write a Makefile tuned to it
learner@embeddex:~/lab$ ./configure --prefix=/opt/yourapp --enable-shared --disable-static
checking for gcc... gcc
configure: prefix set to /opt/yourapp
learner@embeddex:~/lab$ # Compile and link hello from the generated Makefile
learner@embeddex:~/lab$ make
gcc -O2 -Wall -c hello.c
gcc -O2 -Wall -c util.c
gcc -O2 -Wall -o hello hello.o util.o
learner@embeddex:~/lab$ # Stage the install under /tmp/stage instead of /opt/yourapp
learner@embeddex:~/lab$ make install DESTDIR=/tmp/stage
mkdir -p /tmp/stage/opt/yourapp/bin
cp hello /tmp/stage/opt/yourapp/bin/
learner@embeddex:~/lab$

2make

make reads the generated Makefile and compiles and links the program. A Makefile is a list of rules; each rule has a target, the files it depends on, and the commands that build it. The commands are indented with a real tab character, which is a famous source of errors.

makefile
CC     = gcc
CFLAGS = -O2 -Wall

hello: hello.o util.o
    $(CC) $(CFLAGS) -o hello hello.o util.o

hello.o: hello.c util.h
    $(CC) $(CFLAGS) -c hello.c

clean:
    rm -f hello *.o

The first rule says: hello depends on hello.o and util.o, and is built by the command on the next line. make walks the dependencies and only rebuilds a target when a prerequisite is newer, which is what makes incremental builds fast. Automatic variables save typing: $@ is the target, $< the first prerequisite, and $^ all of them. Run make -j$(nproc) to compile in parallel across all your cores.

3make install

make install copies the finished files into the directories under --prefix, typically bin/ for programs, lib/ for libraries and include/ for headers. It needs sudo when the prefix is outside your home directory. There is also make clean, which removes build products, and make uninstall, which some projects provide.

Two more pieces matter for real work. DESTDIR is a variable that make install prepends to every path, so make install DESTDIR=/tmp/stage writes to /tmp/stage/usr/local/bin instead of /usr/local/bin. It is how a build is staged for packaging or for a target board's root filesystem without touching the build machine, and it is the single most important trick for cross-compilation.

bash
make install DESTDIR=/tmp/stage    # stage a root for packaging or a board

Build dependencies are usually not installed by default. On Debian and Ubuntu the build-essential metapackage pulls in the compiler, make and the standard headers. Beyond that, a program that links against a library needs its development package, named with a -dev suffix on Debian and -devel on Red Hat. The development package holds the headers and the unversioned linker symlink, without which you cannot compile against the library even though the runtime library is already installed.

bash
sudo apt install build-essential   # gcc, g++, make and the base headers
sudo apt install libssl-dev        # headers and linker symlink for OpenSSL

Autotools is not the only build system. CMake and Meson are the modern alternatives, and you will meet them in M4 cross-compilation. Their flow has the same three stages under different names.

bash
cmake -S . -B build                # configure
cmake --build build -j             # compile
cmake --install build --prefix /opt/yourapp   # install

meson setup builddir               # configure
meson compile -C builddir          # compile
meson install -C builddir          # install
Source build is the bridge to cross-compilation

On a board you rarely compile in place: the CPU is slow and the image may lack a compiler. Instead you build on a fast PC with a cross toolchain, using ./configure --host=arm-linux-gnueabihf (or the equivalent for your board), a sysroot holding the target's headers and libraries, and DESTDIR to stage the result. Every idea in this section reappears there, which is why it is worth learning the mechanics now.

The configure, make, make install flow./configureprobesystemgenerateMakefilemakecompile andlinkmakeinstallcopy to--prefixDESTDIRstage aroot./configureprobesystemgenerateMakefilemakecompile andlinkmakeinstallcopy to--prefixDESTDIRstage aroot
The configure, make, make install flow, with --prefix and DESTDIR controlling where files land.
02

Shared libraries and their three names

5 min read

A shared library is a file containing compiled code that is not copied into each program. Instead the program records that it needs the library, and the code is loaded and joined to the program when it starts, a process called dynamic linking at load time. The name "shared" is literal: many running programs map the same library's code into their address spaces, and the operating system keeps one copy of the code pages in RAM, which saves memory on a small board.

The three names are the real name, the soname and the linker name. Take a library called libfoo at version 1.2.3.

  • The real name is libfoo.so.1.2.3. This is the actual file on disk, carrying the full version number.
  • The soname is libfoo.so.1. It records only the major version, and it is embedded inside the library file as a tag called DT_SONAME.
  • The linker name is libfoo.so. It has no version and is used only while building, never at run time.

The soname is the crucial invention. When you link a program against the library, the linker reads the soname and writes it into the program as a requirement, a tag called DT_NEEDED. At run time the looks for libfoo.so.1, not libfoo.so.1.2.3. This indirection is what lets a compatible upgrade, say from 1.2.3 to 1.2.4, replace the file without recompiling anything: both provide the same soname libfoo.so.1. A major version change, from 1.x to 2.x, changes the soname to libfoo.so.2, which is exactly the signal that the interface changed incompatibly.

Building a library with all three names takes a few commands. Compile with -fPIC so the code is position independent and can be mapped at any address, then link with -shared and set the soname, then create the two symlinks by hand.

bash
gcc -fPIC -c foo.c -o foo.o
gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.2.3 foo.o
ln -s libfoo.so.1.2.3 libfoo.so.1     # the soname symlink, used at run time
ln -s libfoo.so.1.2.3 libfoo.so       # the linker name, used at build time

Two of those three names are symlinks, and each points at the real file. libfoo.so.1 is what the loader resolves, and libfoo.so is what the compiler resolves when you pass -lfoo; only libfoo.so.1.2.3 is a real file.

bash
gcc main.c -L. -lfoo -o app           # -lfoo resolves the linker name libfoo.so
ldd ./app                             # shows app needs libfoo.so.1

The loader itself is a shared object. On an x86-64 PC it is /lib64/ld-linux-x86-64.so.2; on a 32-bit ARM board it is /lib/ld-linux-armhf.so.3, and on a 64-bit ARM board /lib/ld-linux-aarch64.so.1. Every dynamically linked program records the path to this loader in its headers, and ldd works by asking it to resolve the program's libraries. Knowing the loader's name matters when a program that plainly exists fails to start. On Ubuntu 24.04, bash reports cannot execute: required file not found (older shells said No such file or directory), which means the loader named in the binary does not exist on that system, a classic symptom when you copy a binary built for a different architecture or C library. readelf -l app | grep interpreter shows which loader it asks for.

Going deeperWhy the soname carries only the major version

Inside the ELF file, the soname lives in a dynamic-section entry you can read with readelf -d libfoo.so.1.2.3 | grep SONAME. The convention is that the major version number in the soname changes only when the library's application binary interface, its ABI, changes incompatibly, while minor and patch versions are backward compatible. Because the program recorded libfoo.so.1, an upgrade to libfoo.so.1.9.0 is a drop-in replacement, whereas libfoo.so.2 is a different library the loader treats separately. glibc adds another layer on top, symbol versioning, tagging each exported symbol with a version such as GLIBC_2.2.5, so one library can keep multiple historical versions of the same symbol and old programs keep working. The naming discipline is a social contract: a library author must bump the soname when they break the ABI, and the failure mode for getting it wrong is a program that loads but crashes or misbehaves, not one that fails cleanly.

Step by stepBuilding a shared library with a soname
  1. Compile: run gcc -fPIC -c on the source to get position-independent object files.

  2. Link: run gcc -shared with -Wl,-soname,libfoo.so.1 to produce the real name libfoo.so.1.2.3.

  3. Soname symlink: create libfoo.so.1 pointing at the real file, for the loader.

  4. Linker symlink: create libfoo.so pointing at the real file, for the compiler.

  5. Link the app: compile the program with -lfoo, which records libfoo.so.1 as DT_NEEDED.

A shared library's three nameslibfoo.so.1.2.3real name, the filelibfoo.so.1soname, run timelibfoo.solinker name, build timelibfoo.so.1.2.3real name, the filelibfoo.so.1soname, run timelibfoo.solinker name, build time
The real name libfoo.so.1.2.3 with its soname symlink libfoo.so.1 and linker-name symlink libfoo.so.

Key takeaways

  • The source-build flow is ./configure && make && make install; --prefix sets the location and DESTDIR stages a root.
  • A shared library has three names: the real name libfoo.so.1.2.3, the soname libfoo.so.1, and the linker name libfoo.so.
  • The soname records only the major version, so a compatible minor upgrade is a drop-in replacement and a major bump signals an incompatible ABI change.
  • A development package such as libfoo-dev holds the headers and the unversioned linker symlink needed to compile against a library.
Reflect
  1. Explain to a colleague, in two sentences: build and install a C program from source with configure, make and make install, controlling the location with --prefix and DESTDIR.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Official documentation first. Links checked when the lesson was published.

Find your next step

↑ ↓ to move · Enter to open · Esc to close