Makefiles for assembly

A minimal Makefile

A Makefile for assembly is short: assemble each source to an object, then link the objects. Tabs must be real tabs, not spaces:

program: main.o print.o ld -o program main.o print.o %.o: %.asm nasm -f elf64 -o $@ $<

Pattern rules

The pattern rule %.o: %.asm builds any object from its source: $< is the source, $@ the target. make runs a rule only when the target is older than its prerequisites, so edits rebuild only what changed.

Dependencies

When a macro file changes, every source including it must rebuild. List the .inc files as prerequisites, or generate header dependencies with nasm -M and include them. Clean targets remove objects and the program.

Next: Optimization basics

Article author: Arthur Isaev

Banner image