TExcelTAD
Home › Build Fundamentals: From Source to Binary › Mini Project: A Library Build by Hand

Mini Project: A Library Build by Hand

Time to use the whole course on one small project, with no build tool at all. You will compile and link a three-module C program, wordstats, entirely by hand: one module becomes a static library, one a shared library with a proper soname, and the third is the application that uses both. Then you will draw its build graph, capture the commands in a script, prove what must be rebuilt after a change, and check that the build is reproducible. This is the outcome the track asks of its first course: you can compile and link a C project manually, and you understand every command.

Piece Becomes Lessons
src/strutil/strutil.c libstrutil.a (static) Object Files, Static Libraries
src/stats/stats.c libstats.so.1.0.0 (shared, soname libstats.so.1) Shared Libraries, Sonames, rpath & the Loader
src/app/main.c bin/wordstats, linked against both Linking, Link Errors & Link Order
the build graph a drawing and a model Build Graphs, Incremental Builds
build.sh one command, reproducible Reproducible Builds, Field Notes: Build Hygiene

Every session below starts by recreating the project files (the same heredocs each time), because each session runs in a fresh folder. On your machine, create them once.

Step 1: sources and the build by hand

The project layout keeps headers in include/, each module in its own folder, and every output in build/ (out-of-tree builds). stats.h marks its API with a visibility macro, because the shared library is compiled with -fvisibility=hidden. Read each command before you look at the output: which stage does it run, and why are the flags there?

bash · try it yourself
mkdir -p wordstats/include wordstats/src/strutil wordstats/src/stats wordstats/src/app && cd wordstats
cat > include/strutil.h <<'SRC'
#pragma once
int count_words(const char *line);
void to_upper(char *s);
SRC
cat > include/stats.h <<'SRC'
#pragma once
#define STATS_API __attribute__((visibility("default")))
STATS_API double stats_mean(const int *values, int n);
STATS_API int stats_max(const int *values, int n);
SRC
cat > src/strutil/strutil.c <<'SRC'
#include <ctype.h>
#include "strutil.h"
int count_words(const char *line) {
    int words = 0, in_word = 0;
    for (; *line; line++) {
        if (isspace((unsigned char)*line)) in_word = 0;
        else if (!in_word) { in_word = 1; words++; }
    }
    return words;
}
void to_upper(char *s) { for (; *s; s++) *s = (char)toupper((unsigned char)*s); }
SRC
cat > src/stats/stats.c <<'SRC'
#include "stats.h"
static int sum(const int *v, int n) { int s = 0; for (int i = 0; i < n; i++) s += v[i]; return s; }
double stats_mean(const int *values, int n) { return n ? (double)sum(values, n) / n : 0.0; }
int stats_max(const int *values, int n) {
    int m = n ? values[0] : 0;
    for (int i = 1; i < n; i++) if (values[i] > m) m = values[i];
    return m;
}
SRC
cat > src/app/main.c <<'SRC'
#include <stdio.h>
#include "stats.h"
#include "strutil.h"
int main(void) {
    const char *lines[] = { "make builds what changed", "the linker resolves symbols",
                            "a shared library loads at run time" };
    int counts[3];
    for (int i = 0; i < 3; i++) counts[i] = count_words(lines[i]);
    char title[] = "word stats";
    to_upper(title);
    printf("%s: mean %.2f words, longest line %d words\n", title, stats_mean(counts, 3), stats_max(counts, 3));
    return 0;
}
SRC
mkdir -p build/obj build/lib build/bin
# 1. static library: compile, archive
gcc -std=c17 -Wall -Wextra -O2 -Iinclude -c src/strutil/strutil.c -o build/obj/strutil.o
ar rcs build/lib/libstrutil.a build/obj/strutil.o
# 2. shared library: PIC, hidden by default, soname, no undefined symbols, symlinks
gcc -std=c17 -Wall -Wextra -O2 -Iinclude -fPIC -fvisibility=hidden -c src/stats/stats.c -o build/obj/stats.o
gcc -shared -Wl,-soname,libstats.so.1 -Wl,--no-undefined build/obj/stats.o -o build/lib/libstats.so.1.0.0
ln -s libstats.so.1.0.0 build/lib/libstats.so.1
ln -s libstats.so.1 build/lib/libstats.so
# 3. application: compile, link (users before providers), rpath relative to the binary
gcc -std=c17 -Wall -Wextra -O2 -Iinclude -c src/app/main.c -o build/obj/main.o
gcc build/obj/main.o -Lbuild/lib -lstats -lstrutil -Wl,-rpath,'$ORIGIN/../lib' -o build/bin/wordstats
build/bin/wordstats
find build -mindepth 1 -printf '%P %y\n' | sort | awk '{print ($2 == "l" ? $1 " (symlink)" : $1)}'
WORD STATS: mean 5.00 words, longest line 7 words
bin
bin/wordstats
lib
lib/libstats.so (symlink)
lib/libstats.so.1 (symlink)
lib/libstats.so.1.0.0
lib/libstrutil.a
obj
obj/main.o
obj/stats.o
obj/strutil.o

Check your understanding against the commands:

  • strutil.o is compiled without -fPIC: it only goes into a static library linked into an executable. (If you later wanted to put libstrutil.a inside a shared library, it would need -fPIC.)
  • stats.o is compiled with -fPIC and -fvisibility=hidden; -Wl,--no-undefined makes sure the library doesn't depend on symbols it doesn't link.
  • The two symlinks give the loader its soname (libstats.so.1) and the linker its linker name (libstats.so).
  • On the link line, -lstats -lstrutil come after main.o. -lstrutil finds libstrutil.a because there is no libstrutil.so.
  • -Wl,-rpath,'$ORIGIN/../lib' lets bin/wordstats find its library wherever the build folder is moved, without LD_LIBRARY_PATH.

Step 2: inspect what you built

Confirm, with the tools from the course, that the outputs are what you intended:

bash · try it yourself
mkdir -p wordstats/include wordstats/src/strutil wordstats/src/stats wordstats/src/app && cd wordstats
cat > include/strutil.h <<'SRC'
#pragma once
int count_words(const char *line);
void to_upper(char *s);
SRC
cat > include/stats.h <<'SRC'
#pragma once
#define STATS_API __attribute__((visibility("default")))
STATS_API double stats_mean(const int *values, int n);
STATS_API int stats_max(const int *values, int n);
SRC
cat > src/strutil/strutil.c <<'SRC'
#include <ctype.h>
#include "strutil.h"
int count_words(const char *line) {
    int words = 0, in_word = 0;
    for (; *line; line++) {
        if (isspace((unsigned char)*line)) in_word = 0;
        else if (!in_word) { in_word = 1; words++; }
    }
    return words;
}
void to_upper(char *s) { for (; *s; s++) *s = (char)toupper((unsigned char)*s); }
SRC
cat > src/stats/stats.c <<'SRC'
#include "stats.h"
static int sum(const int *v, int n) { int s = 0; for (int i = 0; i < n; i++) s += v[i]; return s; }
double stats_mean(const int *values, int n) { return n ? (double)sum(values, n) / n : 0.0; }
int stats_max(const int *values, int n) {
    int m = n ? values[0] : 0;
    for (int i = 1; i < n; i++) if (values[i] > m) m = values[i];
    return m;
}
SRC
cat > src/app/main.c <<'SRC'
#include <stdio.h>
#include "stats.h"
#include "strutil.h"
int main(void) {
    const char *lines[] = { "make builds what changed", "the linker resolves symbols",
                            "a shared library loads at run time" };
    int counts[3];
    for (int i = 0; i < 3; i++) counts[i] = count_words(lines[i]);
    char title[] = "word stats";
    to_upper(title);
    printf("%s: mean %.2f words, longest line %d words\n", title, stats_mean(counts, 3), stats_max(counts, 3));
    return 0;
}
SRC
mkdir -p build/obj build/lib build/bin
# 1. static library: compile, archive
gcc -std=c17 -Wall -Wextra -O2 -Iinclude -c src/strutil/strutil.c -o build/obj/strutil.o
ar rcs build/lib/libstrutil.a build/obj/strutil.o
# 2. shared library: PIC, hidden by default, soname, no undefined symbols, symlinks
gcc -std=c17 -Wall -Wextra -O2 -Iinclude -fPIC -fvisibility=hidden -c src/stats/stats.c -o build/obj/stats.o
gcc -shared -Wl,-soname,libstats.so.1 -Wl,--no-undefined build/obj/stats.o -o build/lib/libstats.so.1.0.0
ln -s libstats.so.1.0.0 build/lib/libstats.so.1
ln -s libstats.so.1 build/lib/libstats.so
# 3. application: compile, link (users before providers), rpath relative to the binary
gcc -std=c17 -Wall -Wextra -O2 -Iinclude -c src/app/main.c -o build/obj/main.o
gcc build/obj/main.o -Lbuild/lib -lstats -lstrutil -Wl,-rpath,'$ORIGIN/../lib' -o build/bin/wordstats
build/bin/wordstats
find build -mindepth 1 -printf '%P %y\n' | sort | awk '{print ($2 == "l" ? $1 " (symlink)" : $1)}'
echo "--- what the shared library exports"
nm -D --defined-only build/lib/libstats.so.1.0.0 | awk '{print $3}'
echo "--- what the app needs at run time"
readelf -d build/bin/wordstats | grep -E 'NEEDED|RUNPATH'
echo "--- the static library's code is inside the app"
nm build/bin/wordstats | grep -E ' (T|U) (count_words|to_upper|stats_mean|stats_max)
  


echo "--- where the loader finds everything"
ldd build/bin/wordstats | sed 's/ (0x[0-9a-f]*)//'
WORD STATS: mean 5.00 words, longest line 7 words
bin
bin/wordstats
lib
lib/libstats.so (symlink)
lib/libstats.so.1 (symlink)
lib/libstats.so.1.0.0
lib/libstrutil.a
obj
obj/main.o
obj/stats.o
obj/strutil.o
--- what the shared library exports
stats_max
stats_mean
--- what the app needs at run time
 0x0000000000000001 (NEEDED)             Shared library: [libstats.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000001d (RUNPATH)            Library runpath: [$ORIGIN/../lib]
--- the static library's code is inside the app
00000000000012e0 T count_words
                 U stats_max
                 U stats_mean
0000000000001350 T to_upper
--- where the loader finds everything
	linux-vdso.so.1
	libstats.so.1 => /home/learner/wordstats/build/bin/../lib/libstats.so.1
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6
	/lib64/ld-linux-x86-64.so.2
  • The shared library exports exactly the two API functions; the helper sum stays hidden.
  • The app records the soname libstats.so.1 and a RUNPATH, and nothing about libstrutil.
  • count_words and to_upper are defined (T) inside the app: static linking copied them in. stats_mean and stats_max are undefined (U): they will come from the shared library at run time.

Step 3: draw the build graph

Draw the graph on paper first: every file is a node, every command an edge from its inputs to its outputs. Then compare with this model, which encodes the same graph, prints a valid order of commands, lists what each edit makes stale, and writes the graph in Graphviz DOT format (paste the DOT text into any Graphviz viewer to see the picture):

js · try it yourself
const graph = {   // output: [inputs]
  "obj/strutil.o": ["src/strutil/strutil.c", "include/strutil.h"],
  "lib/libstrutil.a": ["obj/strutil.o"],
  "obj/stats.o": ["src/stats/stats.c", "include/stats.h"],
  "lib/libstats.so.1.0.0": ["obj/stats.o"],
  "obj/main.o": ["src/app/main.c", "include/stats.h", "include/strutil.h"],
  "bin/wordstats": ["obj/main.o", "lib/libstats.so.1.0.0", "lib/libstrutil.a"],
};

// 1. A topological order of the build steps (depth-first, inputs first)
const order = [], seen = new Set();
(function visit(n) {
  if (seen.has(n)) return;
  seen.add(n);
  for (const i of graph[n] ?? []) visit(i);
  if (graph[n]) order.push(n);
})("bin/wordstats");
console.log("build order:\n  " + order.join("\n  "));

// 2. What each source edit makes stale (everything downstream)
const users = {};
for (const [out, ins] of Object.entries(graph)) for (const i of ins) (users[i] ??= []).push(out);
function stale(file) {
  const found = new Set(), todo = [file];
  while (todo.length) for (const u of users[todo.pop()] ?? []) if (!found.has(u)) { found.add(u); todo.push(u); }
  return [...found];
}
for (const f of ["src/stats/stats.c", "src/strutil/strutil.c", "include/stats.h"]) console.log(`edit ${f}: ${stale(f).join(", ")}`);

// 3. Graphviz DOT
console.log("digraph wordstats {");
for (const [out, ins] of Object.entries(graph)) for (const i of ins) console.log(`  "${i}" -> "${out}";`);
console.log("}");
build order:
  obj/main.o
  obj/stats.o
  lib/libstats.so.1.0.0
  obj/strutil.o
  lib/libstrutil.a
  bin/wordstats
edit src/stats/stats.c: obj/stats.o, lib/libstats.so.1.0.0, bin/wordstats
edit src/strutil/strutil.c: obj/strutil.o, lib/libstrutil.a, bin/wordstats
edit include/stats.h: obj/stats.o, obj/main.o, bin/wordstats, lib/libstats.so.1.0.0
digraph wordstats {
  "src/strutil/strutil.c" -> "obj/strutil.o";
  "include/strutil.h" -> "obj/strutil.o";
  "obj/strutil.o" -> "lib/libstrutil.a";
  "src/stats/stats.c" -> "obj/stats.o";
  "include/stats.h" -> "obj/stats.o";
  "obj/stats.o" -> "lib/libstats.so.1.0.0";
  "src/app/main.c" -> "obj/main.o";
  "include/stats.h" -> "obj/main.o";
  "include/strutil.h" -> "obj/main.o";
  "obj/main.o" -> "bin/wordstats";
  "lib/libstats.so.1.0.0" -> "bin/wordstats";
  "lib/libstrutil.a" -> "bin/wordstats";
}

The model treats bin/wordstats as depending on libstats.so.1.0.0. That is true for linking (the linker reads the library's symbols), but the app doesn't need to be relinked when only the code inside the library changes. Precise build tools tell the two apart: they relink against a shared library only when its exported interface changes. Step 4 shows the difference for real.

Step 4: one script, and what a change really costs

Put the commands in build.sh, so the build is one command, and make the script echo each command (like make does). Then make two changes and watch what each one requires:

bash · try it yourself
mkdir -p wordstats/include wordstats/src/strutil wordstats/src/stats wordstats/src/app && cd wordstats
cat > include/strutil.h <<'SRC'
#pragma once
int count_words(const char *line);
void to_upper(char *s);
SRC
cat > include/stats.h <<'SRC'
#pragma once
#define STATS_API __attribute__((visibility("default")))
STATS_API double stats_mean(const int *values, int n);
STATS_API int stats_max(const int *values, int n);
SRC
cat > src/strutil/strutil.c <<'SRC'
#include <ctype.h>
#include "strutil.h"
int count_words(const char *line) {
    int words = 0, in_word = 0;
    for (; *line; line++) {
        if (isspace((unsigned char)*line)) in_word = 0;
        else if (!in_word) { in_word = 1; words++; }
    }
    return words;
}
void to_upper(char *s) { for (; *s; s++) *s = (char)toupper((unsigned char)*s); }
SRC
cat > src/stats/stats.c <<'SRC'
#include "stats.h"
static int sum(const int *v, int n) { int s = 0; for (int i = 0; i < n; i++) s += v[i]; return s; }
double stats_mean(const int *values, int n) { return n ? (double)sum(values, n) / n : 0.0; }
int stats_max(const int *values, int n) {
    int m = n ? values[0] : 0;
    for (int i = 1; i < n; i++) if (values[i] > m) m = values[i];
    return m;
}
SRC
cat > src/app/main.c <<'SRC'
#include <stdio.h>
#include "stats.h"
#include "strutil.h"
int main(void) {
    const char *lines[] = { "make builds what changed", "the linker resolves symbols",
                            "a shared library loads at run time" };
    int counts[3];
    for (int i = 0; i < 3; i++) counts[i] = count_words(lines[i]);
    char title[] = "word stats";
    to_upper(title);
    printf("%s: mean %.2f words, longest line %d words\n", title, stats_mean(counts, 3), stats_max(counts, 3));
    return 0;
}
SRC
cat > build.sh <<'SH'
#!/bin/bash
# Hand-written build for wordstats: static libstrutil, shared libstats, app.
set -e
OUT=${OUT:-build}
MAP="-ffile-prefix-map=$PWD=."; [ -n "$NO_MAP" ] && MAP=""      # NO_MAP=1 turns the path fix off
CFLAGS="-std=c17 -Wall -Wextra -O2 -g -Iinclude $MAP"
mkdir -p $OUT/obj $OUT/lib $OUT/bin
run() { echo "+ $*"; "$@"; }
# 1. static library
run gcc $CFLAGS -c src/strutil/strutil.c -o $OUT/obj/strutil.o
run ar rcs $OUT/lib/libstrutil.a $OUT/obj/strutil.o
# 2. shared library: PIC, hidden by default, soname, no undefined symbols
run gcc $CFLAGS -fPIC -fvisibility=hidden -c src/stats/stats.c -o $OUT/obj/stats.o
run gcc -shared -Wl,-soname,libstats.so.1 -Wl,--no-undefined $OUT/obj/stats.o -o $OUT/lib/libstats.so.1.0.0
run ln -sf libstats.so.1.0.0 $OUT/lib/libstats.so.1
run ln -sf libstats.so.1 $OUT/lib/libstats.so
# 3. the application: users before providers, rpath relative to the binary
run gcc $CFLAGS -c src/app/main.c -o $OUT/obj/main.o
run gcc $OUT/obj/main.o -L$OUT/lib -lstats -lstrutil -Wl,-rpath,'$ORIGIN/../lib' -o $OUT/bin/wordstats
SH
chmod +x build.sh
./build.sh > /dev/null && build/bin/wordstats
cp build/bin/wordstats app-before
echo "--- 1. change the SHARED library: rebuild only libstats"
cp src/stats/stats.c stats.c.orig
sed -i 's/return n ? (double)sum(values, n) \/ n : 0.0;/return n ? (double)sum(values, n) \/ n + 100 : 0.0;   \/* a bug *\//' src/stats/stats.c
gcc -std=c17 -O2 -Iinclude -fPIC -fvisibility=hidden -c src/stats/stats.c -o build/obj/stats.o
gcc -shared -Wl,-soname,libstats.so.1 -Wl,--no-undefined build/obj/stats.o -o build/lib/libstats.so.1.0.0
cmp -s app-before build/bin/wordstats && echo "app binary unchanged, but its output changed:"
build/bin/wordstats
echo "--- 2. change the STATIC library: the app must be relinked"
cp stats.c.orig src/stats/stats.c && ./build.sh > /dev/null     # undo change 1
sed -i 's/int words = 0, in_word = 0;/int words = 1, in_word = 0;   \/* off by one *\//' src/strutil/strutil.c
gcc -std=c17 -O2 -Iinclude -c src/strutil/strutil.c -o build/obj/strutil.o
ar rcs build/lib/libstrutil.a build/obj/strutil.o
printf 'without relinking: '; build/bin/wordstats
gcc build/obj/main.o -Lbuild/lib -lstats -lstrutil -Wl,-rpath,'$ORIGIN/../lib' -o build/bin/wordstats
printf 'after relinking:   '; build/bin/wordstats
WORD STATS: mean 5.00 words, longest line 7 words
--- 1. change the SHARED library: rebuild only libstats
app binary unchanged, but its output changed:
WORD STATS: mean 105.00 words, longest line 7 words
--- 2. change the STATIC library: the app must be relinked
without relinking: WORD STATS: mean 5.00 words, longest line 7 words
after relinking:   WORD STATS: mean 6.00 words, longest line 8 words
  1. A change inside the shared library takes effect after rebuilding only libstats.so.1.0.0: the app binary is byte-for-byte the same, yet it now prints the buggy mean. That's the strength (fix a bug once for every program) and the danger (break every program at once) of shared libraries.
  2. A change inside the static library has no effect until the app is relinked, because the old count_words was copied into the executable. A build tool that forgets this edge ships the old code.

Step 5: is the build reproducible?

Copy the project to a second folder, build both, and compare the outputs byte by byte. The script compiles with -g, so the folder name ends up in the debug info unless -ffile-prefix-map rewrites it:

bash · try it yourself
mkdir -p wordstats/include wordstats/src/strutil wordstats/src/stats wordstats/src/app && cd wordstats
cat > include/strutil.h <<'SRC'
#pragma once
int count_words(const char *line);
void to_upper(char *s);
SRC
cat > include/stats.h <<'SRC'
#pragma once
#define STATS_API __attribute__((visibility("default")))
STATS_API double stats_mean(const int *values, int n);
STATS_API int stats_max(const int *values, int n);
SRC
cat > src/strutil/strutil.c <<'SRC'
#include <ctype.h>
#include "strutil.h"
int count_words(const char *line) {
    int words = 0, in_word = 0;
    for (; *line; line++) {
        if (isspace((unsigned char)*line)) in_word = 0;
        else if (!in_word) { in_word = 1; words++; }
    }
    return words;
}
void to_upper(char *s) { for (; *s; s++) *s = (char)toupper((unsigned char)*s); }
SRC
cat > src/stats/stats.c <<'SRC'
#include "stats.h"
static int sum(const int *v, int n) { int s = 0; for (int i = 0; i < n; i++) s += v[i]; return s; }
double stats_mean(const int *values, int n) { return n ? (double)sum(values, n) / n : 0.0; }
int stats_max(const int *values, int n) {
    int m = n ? values[0] : 0;
    for (int i = 1; i < n; i++) if (values[i] > m) m = values[i];
    return m;
}
SRC
cat > src/app/main.c <<'SRC'
#include <stdio.h>
#include "stats.h"
#include "strutil.h"
int main(void) {
    const char *lines[] = { "make builds what changed", "the linker resolves symbols",
                            "a shared library loads at run time" };
    int counts[3];
    for (int i = 0; i < 3; i++) counts[i] = count_words(lines[i]);
    char title[] = "word stats";
    to_upper(title);
    printf("%s: mean %.2f words, longest line %d words\n", title, stats_mean(counts, 3), stats_max(counts, 3));
    return 0;
}
SRC
cat > build.sh <<'SH'
#!/bin/bash
# Hand-written build for wordstats: static libstrutil, shared libstats, app.
set -e
OUT=${OUT:-build}
MAP="-ffile-prefix-map=$PWD=."; [ -n "$NO_MAP" ] && MAP=""      # NO_MAP=1 turns the path fix off
CFLAGS="-std=c17 -Wall -Wextra -O2 -g -Iinclude $MAP"
mkdir -p $OUT/obj $OUT/lib $OUT/bin
run() { echo "+ $*"; "$@"; }
# 1. static library
run gcc $CFLAGS -c src/strutil/strutil.c -o $OUT/obj/strutil.o
run ar rcs $OUT/lib/libstrutil.a $OUT/obj/strutil.o
# 2. shared library: PIC, hidden by default, soname, no undefined symbols
run gcc $CFLAGS -fPIC -fvisibility=hidden -c src/stats/stats.c -o $OUT/obj/stats.o
run gcc -shared -Wl,-soname,libstats.so.1 -Wl,--no-undefined $OUT/obj/stats.o -o $OUT/lib/libstats.so.1.0.0
run ln -sf libstats.so.1.0.0 $OUT/lib/libstats.so.1
run ln -sf libstats.so.1 $OUT/lib/libstats.so
# 3. the application: users before providers, rpath relative to the binary
run gcc $CFLAGS -c src/app/main.c -o $OUT/obj/main.o
run gcc $OUT/obj/main.o -L$OUT/lib -lstats -lstrutil -Wl,-rpath,'$ORIGIN/../lib' -o $OUT/bin/wordstats
SH
chmod +x build.sh
same() { cmp -s "$1" "$2" && echo "identical" || echo "different"; }
cd .. && cp -r wordstats wordstats-copy
compare() {
  (cd wordstats && rm -rf build && ./build.sh > /dev/null)
  (cd wordstats-copy && rm -rf build && ./build.sh > /dev/null)
  for f in bin/wordstats lib/libstats.so.1.0.0 lib/libstrutil.a; do
    printf '  %-22s ' $f; same wordstats/build/$f wordstats-copy/build/$f
  done
}
echo "with -ffile-prefix-map:"; compare
export NO_MAP=1
echo "without it (NO_MAP=1):"; compare
with -ffile-prefix-map:
  bin/wordstats          identical
  lib/libstats.so.1.0.0  identical
  lib/libstrutil.a       identical
without it (NO_MAP=1):
  bin/wordstats          different
  lib/libstats.so.1.0.0  different
  lib/libstrutil.a       different

With the prefix map, two checkouts in different folders produce identical outputs. Without it, the outputs that contain debug info differ, only because of where they were built.

Extensions

Pick one or more to go further:

  1. Make it incremental. Replace build.sh with a Makefile using pattern rules, -MMD -MP header dependencies and the out-of-tree build/ folder. Check: touching include/stats.h must rebuild stats.o and main.o but not strutil.o. (GNU Make & Ninja is the next course.)
  2. Ship a soname bump. Change stats_max to take an extra parameter, release it as libstats.so.2, and show that an old binary still runs against libstats.so.1 installed next to it.
  3. Load stats as a plugin. Keep strutil static, but load libstats.so.1 with dlopen and dlsym at run time, with a clear error message when it's missing.
  4. Cross-compile the static library for aarch64-linux-gnu with clang --target. Does strutil.c compile without a sysroot? (It includes <ctype.h>.) Find out, and explain the result.
  5. Package it. Create a reproducible wordstats-1.0.0.tar.gz of bin/ and lib/ with fixed order, times and owners, and verify that the hash is the same on two builds.

Test yourself

After changing only src/stats/stats.c and rebuilding libstats.so.1.0.0, why does the app's output change without relinking?

  1. The app was rebuilt automatically
  2. The app loads libstats.so.1 at run time, so it uses the new library code
  3. Static linking copied the new code
  4. LD_LIBRARY_PATH was set

Why did the change to libstrutil.a have no effect until the app was relinked?

  1. Static libraries are loaded lazily
  2. The linker copied count_words into the executable at link time
  3. The archive index was stale
  4. The change was in a comment

Which flag made the two builds in different folders identical?

  1. -O2
  2. -fvisibility=hidden
  3. -ffile-prefix-map=$PWD=.
  4. -Wl,--no-undefined