0Pricing
C Academy · Lesson

Makefiles

Automate the build.

Makefiles is a free C Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Makefiles

Typing gcc commands by hand gets unmanageable fast. make reads a Makefile describing how to build your project and runs only the commands needed.

Its killer feature: it rebuilds a file only when its inputs are newer than its output, saving huge amounts of compile time.

Rule Anatomy

A rule has three parts: a target, its prerequisites, and a recipe.

The recipe line MUST begin with a real TAB character, not spaces. This is the single most common Makefile mistake.

target: prerequisite1 prerequisite2
	<TAB>command to build target

A First Makefile

This builds app from two source files. Running make with no argument builds the first target.

If neither source changed since the last build, make prints 'up to date' and does nothing.

app: main.c math_utils.c
	gcc main.c math_utils.c -o app

Separate Compilation Rules

Building each object separately means a change in one file recompiles only that file.

make follows the dependency graph: change main.c and only main.o plus the final link rerun.

app: main.o math_utils.o
	gcc main.o math_utils.o -o app

main.o: main.c math_utils.h
	gcc -c main.c

math_utils.o: math_utils.c math_utils.h
	gcc -c math_utils.c

Variables

Variables remove repetition. Define them at the top and expand with $(NAME).

Now changing the compiler or flags happens in one place.

CC = gcc
CFLAGS = -Wall -Wextra -g

app: main.o math_utils.o
	$(CC) main.o math_utils.o -o app

Automatic Variables

make provides shorthands inside recipes:

  • $@ — the target name
  • $^ — all prerequisites
  • $< — the first prerequisite

They keep recipes short and correct.

app: main.o math_utils.o
	$(CC) $^ -o $@

main.o: main.c
	$(CC) $(CFLAGS) -c $< -o $@

Pattern Rules

One pattern rule compiles every .c into its .o, so you stop writing a rule per file.

% matches any stem; combined with $< and $@ it is fully generic.

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

Phony Targets

Targets like clean do not produce a file of that name. Declare them .PHONY so make always runs them, even if a file named 'clean' exists.

clean removes build artifacts to force a fresh build.

.PHONY: clean
clean:
	rm -f *.o app

A Complete Makefile

Putting it together: variables, a pattern rule, a link rule, and a phony clean.

This scales to many files with no per-file edits.

CC = gcc
CFLAGS = -Wall -Wextra -g
OBJS = main.o math_utils.o

app: $(OBJS)
	$(CC) $^ -o $@

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

.PHONY: clean
clean:
	rm -f $(OBJS) app

How make Decides

make compares timestamps. A target is rebuilt if any prerequisite has a newer modification time, or if the target does not exist.

This is why listing header dependencies matters: edit a header and every dependent object correctly rebuilds.

Multiple Targets And Defaults

The first target in a Makefile is the default, run when you type plain make. By convention it is named all.

You can also build a specific target by name, like make clean or make test, giving one Makefile many useful entry points.

.PHONY: all
all: app

test: app
	./app --selftest

Quick Check

Diagnose a classic Makefile error.

Recap

You can now automate builds with make:

  • Rules are target, prerequisites, and a TAB-indented recipe
  • Variables and automatic variables ($@ $^ $<) cut repetition
  • Pattern rules (%.o: %.c) handle every file generically
  • .PHONY marks non-file targets like clean
  • make rebuilds only what is out of date by timestamp

Next: building static and shared libraries.

Frequently asked questions

Is the “Makefiles” lesson free?

Yes — the full text of “Makefiles” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Makefiles”?

Automate the build. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Makefiles” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Conditional Compilation
  2. Multi-File Projects
  3. Makefiles
  4. Static and Shared Libraries
← Back to C Academy