Skip to main content

Lesson 5 of 5

Mini Project Part 9 - Makefile Project Setup

Set up the complete multi-file build for the growing Task Manager project using the professional Makefile template. The project now has: `arena.c`, `list.c`, `db.c`, `main.c` - compiled separately with a `debug` and `release` target, auto-generated dependencies, and a `test` target running valgrind.

14 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Complete Project Makefile with Auto-Dependencies

Complete Project Makefile with Auto-Dependencies
# Project structure:
# task-manager/
# ├── Makefile
# ├── include/
# │   ├── arena.h
# │   ├── list.h
# │   └── db.h
# └── src/
#     ├── arena.c
#     ├── list.c
#     ├── db.c
#     └── main.c

CC      = gcc
CFLAGS  = -Wall -Wextra -Wpedantic -std=c99 -Iinclude
LDFLAGS =
LIBS    =

# ── Source discovery ─────────────────────────────────────────
SRCDIR = src
OBJDIR = build
SRCS   = $(wildcard $(SRCDIR)/*.c)
OBJS   = $(SRCS:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
DEPS   = $(OBJS:.o=.d)

# ── Build modes ──────────────────────────────────────────────
ifdef DEBUG
  CFLAGS  += -g3 -O0 -fsanitize=address,undefined -fno-omit-frame-pointer
  LDFLAGS += -fsanitize=address,undefined
  TARGET  = $(OBJDIR)/task-manager-debug
else
  CFLAGS  += -O2 -DNDEBUG
  TARGET  = $(OBJDIR)/task-manager
endif

# ── Targets ──────────────────────────────────────────────────
.PHONY: all debug clean run test

all: $(OBJDIR) $(TARGET)

debug:
	$(MAKE) DEBUG=1

# ── Create build directory ───────────────────────────────────
$(OBJDIR):
	mkdir -p $(OBJDIR)

# ── Link ─────────────────────────────────────────────────────
$(TARGET): $(OBJS)
	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
	@echo "Built: $@"

# ── Compile + auto-dependency generation ─────────────────────
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
	$(CC) $(CFLAGS) -MMD -MP -c -o $@ $<
#   -MMD: write .d dependency file alongside .o
#   -MP:  add phony target for each dep to survive file deletion

# ── Include auto-generated dependencies ──────────────────────
-include $(DEPS)

# ── Helper targets ───────────────────────────────────────────
clean:
	rm -rf $(OBJDIR)

run: all
	$(TARGET)

test: DEBUG=1
test: all
	@echo "--- valgrind ---"
	valgrind --leak-check=full --error-exitcode=1 	         --track-origins=yes $(TARGET)

# ── Show compile commands ────────────────────────────────────
# make VERBOSE=1   (add: ifeq ($(VERBOSE),1) at the recipe level)

# ── Usage ────────────────────────────────────────────────────
# make            → release build in build/
# make debug      → debug build with ASAN
# make run        → build and run
# make test       → build debug + valgrind
# make clean      → remove build/
# make -j$(nproc) → parallel build using all CPU cores

Common Mistakes

  • Not creating the object directory - `$(CC) -c -o build/main.o src/main.c` fails if `build/` doesn't exist. The `| $(OBJDIR)` order-only prerequisite ensures the directory is created before any `.o` file.
  • Using `CFLAGS` for linker flags - ASAN must be passed to BOTH the compiler (`-fsanitize=...` in CFLAGS) AND the linker (`-fsanitize=...` in LDFLAGS or appended to the link command). Omitting it from the link step causes sanitizer runtime not to be linked.
  • `-include $(DEPS)` vs `include $(DEPS)` - with `include`, Make errors if a `.d` file is missing (first build). With `-include`, missing files are silently ignored - correct for first build when no `.d` files exist yet.

Tip

Tip

Practice Mini Project Part 9 Makefile Project Setup in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Standard project directory layout separates headers, source files, and compiled artifacts

Practice Task

Note

Practice Task - (1) Write a working example of Mini Project Part 9 Makefile Project Setup from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Mini Project Part 9 Makefile Project Setup is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • Set up the complete multi-file build for the growing Task Manager project using the professional Makefile template.
  • Not creating the object directory - `$(CC) -c -o build/main.o src/main.c` fails if `build/` doesn't exist. The `| $(OBJDIR)` order-only prerequisite ensures the directory is created before any `.o` file.
  • Using `CFLAGS` for linker flags - ASAN must be passed to BOTH the compiler (`-fsanitize=...` in CFLAGS) AND the linker (`-fsanitize=...` in LDFLAGS or appended to the link command). Omitting it from the link step causes sanitizer runtime not to be linked.
  • `-include $(DEPS)` vs `include $(DEPS)` - with `include`, Make errors if a `.d` file is missing (first build). With `-include`, missing files are silently ignored - correct for first build when no `.d` files exist yet.

Topics in This Module

0/5 done