Build objects into a separate output directory.
Section: Implicit and pattern rules
Pattern rule with directories
makefile
makefile
build/%.o: src/%.c
mkdir -p $(dir $@)
cc -c $< -o $@Explanation
Excellent for out-of-tree builds.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Implicit and pattern rules
Link binary from generated object list
Assemble final executable from all objects.
Clear built-in suffix rules
Reduce surprises by disabling legacy suffix rules.
$* stem in pattern rules
Use the matched stem from the target pattern.