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.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What must sit in front of a recipe line in a Makefile?
Answer
A real tab
A tab marks the line as a recipe command; spaces give "missing separator".
-
What does
$@mean in a make rule?Answer
The target of the rule
$@ is the target, and $< is the first prerequisite.
-
What does
$<mean in a pattern rule?Answer
The first prerequisite
In
%.o: %.asm, $< is the .asm file that matched the pattern. -
When does make run a rule?
Answer
When the target is older than a prerequisite, or does not exist
That timestamp comparison is the whole point of make.
-
Which rule builds any object from its source?
Answer
%.o: %.asm
The percent sign matches the file stem, so one rule covers every source.
-
Why list .inc files as prerequisites?
Answer
A change in an include must rebuild the sources that use it
make sees only the prerequisites you name, and an include is a dependency.
-
What is a usual clean target?
Answer
rm -f *.o program
clean removes the build products so the next make starts from scratch.
-
How can NASM generate header dependencies for make?
Answer
nasm -M
-M prints make rules for the include files a source uses.
-
What is the first target of a Makefile?
Answer
The default goal make builds
make builds the first target when no goal is named on the command line.
-
What does
nasm -f elf64 -o $@ $<do inside the pattern rule?Answer
It assembles the matched source into the matched object
$@ is the .o target and $< the .asm prerequisite.
The Makefile on this page was run with GNU Make 4.4.1 and NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev