Make reference
Automatic Variables
$@ # Target file name
$% # Target member name (what matches % in the target)
$< # First prerequisite
$? # All the prerequisites that are newer than the target
$^ # All the prerequisites
$+ # Like $^ but with duplicates
$| # Order-only prerequisites
$* # The stem (% in the target) with which an implicit rule matches
%.ext: %.src
@command $? > $@
File Name Functions
"Each file name in the series is transformed in the same way and the results are concatenated with single spaces between them."
$(dir names...) # Extracts the directory-part
$(notdir names...) # Extracts all but the directory-part
$(suffix names...) # Extracts the suffix
$(basename names...) # Extracts all but the suffix
$(addsuffix suffix, names...) # The suffix is appended to the end
$(addprefix prefix, names...) # The prefix is prepended to the front
$(join list1, list2) # Concatenates the two arguments word by word
$(wildcard pattern) # File expansion
$(realpath names...) # Return the canonical absolute name (file must exist)
$(abspath names...) # Return an absolute name (file may not exist)
Text Functions
$(subst from, to, text) # Replace 'from' with 'to' on 'text'
$(patsubst pattern, to, text) # Pattern substitution for each word (% as wildcard)
$(strip string) # Removes leading/trailing whitespace
$(findstring needle, haystack) # Test for occurrence of 'needle' in 'haystack'
$(filter pattern..., text) # Removes words that do match 'pattern' (supports % wildcard)
$(filter-out pattern..., text) # Inverse of the above
$(sort list) # Sorts the words in lexical order, removing duplicates
$(word n, text) # Nth word of 'text'
$(wordlist s, e, text) # List of words from index 's' to 'e'
$(words text) # Number of words in 'text'
$(firstword names...) # First word
$(lastword names...) # Last word
The patsubst
function has syntactic sugar in the form of:
$(foo:from=to)
$(foo:%.o=%.c)
$(foo:.o=.c) # % is implicit
Setting Variables
var1 = foo bar # Recursively expanded variable
var2 := foo bar # Simply expanded variable
var3 ?= foo bar # Only assign the value if not yet defined
Special targets
.PHONY: # Prerequisites are not file names (recipies always run)
.INTERMEDIATE: # Prerequisites files are deleted after no longer needed
.SECONDARY: # Intermediate files that are not to be deleted
.SILENT: # Do not print recipe commands