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?
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.ois compiled without-fPIC: it only goes into a static library linked into an executable. (If you later wanted to putlibstrutil.ainside a shared library, it would need-fPIC.)stats.ois compiled with-fPICand-fvisibility=hidden;-Wl,--no-undefinedmakes 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 -lstrutilcome aftermain.o.-lstrutilfindslibstrutil.abecause there is nolibstrutil.so. -Wl,-rpath,'$ORIGIN/../lib'letsbin/wordstatsfind its library wherever thebuildfolder is moved, withoutLD_LIBRARY_PATH.
Step 2: inspect what you built
Confirm, with the tools from the course, that the outputs are what you intended:
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)