Skip to content
AgileLogics
4 min read

Build Tools: Make

Prerequisites: Stacks & queues

Typing out a gcc command with every source file by hand gets old fast — a Makefile automates it, and only recompiles files that actually changed:

CC = gcc
CFLAGS = -Wall -Wextra

myprogram: main.o math_utils.o
    $(CC) $(CFLAGS) -o myprogram main.o math_utils.o

main.o: main.c math_utils.h
    $(CC) $(CFLAGS) -c main.c

math_utils.o: math_utils.c math_utils.h
    $(CC) $(CFLAGS) -c math_utils.c

clean:
    rm -f *.o myprogram
make          # builds myprogram
make clean    # removes build artifacts

(CMake, covered in Level 3, is a more modern, cross-platform alternative that generates a Makefile — or an equivalent — for you.)