You have done the workshop loop. The five tools are not abstract any more. You know what each one is for, when it speaks up, and what it prints when it does.
| Tool | What it caught |
|---|---|
gcc -Wall -Wextra -g | Nothing this round — but it can. |
gdb (run, bt) | A segfault, with the file and line. |
valgrind | A memory leak, with the allocation site. |
-fsanitize=address | A heap buffer overflow, with both stacks. |
-fsanitize=undefined | Standing by for signed overflows and friends. |
You also wrote a Makefile, split a single file into a real project,
and made make recompile only what changed.
This is what building C means.
Reading Doom's Makefile
You earned the right to clone the Doom source and read its build file. Do it now:
git clone https://github.com/id-Software/DOOM.git
cd DOOM/linuxdoom-1.10
less MakefileThe first lines look familiar:
CC= gcc # gcc or g++
CFLAGS=-g -Wall -DNORMALUNIX -DLINUX # -DUSEASM
LDFLAGS=-L/usr/X11R6/lib
LIBS=-lXext -lX11 -lnsl -lmSame CC, same CFLAGS, same -g and -Wall you have been
using. -DNORMALUNIX and -DLINUX define preprocessor symbols so
the C code can #ifdef LINUX for platform-specific paths;
-DUSEASM is commented out — a switch the original developers
used to toggle hand-written assembly paths in or out.
LDFLAGS=-L/usr/X11R6/lib is the -L flag from earlier in this
chapter — it adds X11's library directory to the linker search
path. LIBS=-lXext -lX11 -lnsl -lm is -l four times — link
against libXext, libX11, libnsl, and libm (the math
library). The full toolchain you used for sort, written by id
Software for a 1995 game, with two more libraries in the link
line.
The recipe section, further down, has the same shape as yours:
$(O)/linuxxdoom: $(OBJS) $(O)/i_main.o
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(O)/i_main.o \
-o $(O)/linuxxdoom $(LIBS)$(OBJS) is a long list of .o files, one per source — the same
shape as your OBJS = $(SRCS:.c=.o). The link command takes them
all, names the output, and links against the libraries.
Doom is bigger than sort. The Makefile is longer. The pieces are the same.
What is next
You have a working C development environment. The c-tier starts
right after this page. The first chapter — c01 — is libtci: you
will spend it building a small standard-library replacement,
function by function, starting with tci_strlen and ending with
tci_atoi.
Every line of code you write in c01 will be compiled with
-Wall -Wextra -g, built with a Makefile, debugged with gdb
when it crashes, checked with valgrind when it leaks, and
hardened with the sanitisers when it does something weird.
The workshop is yours. The real curriculum starts now.