Skip to content

Latest commit

 

History

History
37 lines (30 loc) · 1.34 KB

File metadata and controls

37 lines (30 loc) · 1.34 KB

Step 5: Build System (Makefile)

A Build System automates the process of converting human-readable source code into machine-executable binaries. In C, Make is the standard tool for this.

Why use Make?

  1. Automation: Instead of typing gcc src/scanner.c src/tokenizer.c ... every time, you just type make.
  2. Efficiency: Make only recompiles files that have changed. It checks the timestamps of the source files vs the object files.
  3. Dependency Management: It ensures libraries are built before the programs that link to them.

Makefile Breakdown

Variables

CC = gcc
CFLAGS = -Iinclude -Wall -Wextra -std=c99
  • Defines the compiler and flags once. If we want to switch to clang, we change it in one place.
  • -Iinclude tells the compiler: "If you see #include "header.h", look in the include folder."

Static Library (.a)

ar rcs $@ $^
  • ar: Archiver tool.
  • rcs: Replace, Create, Sort-index.
  • Creates a "bag" of .o files. This is your library.

Phony Targets

.PHONY: all clean
  • Tells Make: "clean is not a file name. It's an action." consistency.

What you learned

  • How to structure a Makefile for a C project.
  • The difference between compiling (.c -> .o) and archiving (.o -> .a).
  • How to separate build artifacts (build/) from source code.