Catalog / Makefile Cheatsheet
Makefile Cheatsheet
A comprehensive cheat sheet for Makefiles, covering syntax, variables, rules, functions, and command prefixes, along with practical examples.
Makefile Basics
Syntax Overview
A Makefile consists of rules, variables, and directives. General Structure:
|
Comments:
|
Including Makefiles:
|
Variables
Variable Assignment |
|
Variable Usage |
|
Example |
|
Rules
Explicit Rule |
|
Implicit Rule |
|
Pattern Rule |
|
Advanced Features
Functions
String Functions |
|
File Name Functions |
|
Conditional Functions |
|
Directives
Conditional Directives |
|
Include Directive |
|
Override Directive |
|
Command Execution
Commands are executed by the shell. Each command is executed in a separate subshell. Example:
|
Use Example:
|
Common Patterns & Best Practices
Target-Specific Variable Values
You can define variable values that are specific to a target. Syntax:
Example:
|
Pattern-Specific Variable Values
You can define variable values that are specific to a pattern of targets. Syntax:
This sets |
Order-only Prerequisites
Order-only prerequisites are listed after a pipe symbol Syntax:
Example:
|
Phony Targets
Phony targets are targets that do not represent actual files. They are typically used to define actions like Syntax:
Example:
|
Debugging and Options
Makefile Options
|
Starts make process. |
|
Specifies the makefile to use. |
|
Prints the commands that would be executed, without actually executing them (dry run). |
|
Unconditionally make all targets. |
|
Specifies the number of jobs to run simultaneously. If N is omitted, make runs as many jobs simultaneously as possible. |
|
Continue as much as possible after an error. |
Debugging Tips
Use |
Example Makefile
|