Cheat sheet with commonly used constructions in Makefiles based on GNU Make
Make command line options
Select the target to run
Select which file to read
Mark file as 'out of date'
Change directory before making
Print all debug information
Print actions without making
Mark encountered targets as 'up of date'
Expand makefile and print
Consider all targets as 'out-of-date'
Name of the target of the recipe being run*
The target member name, when the target is an archive member*
Name of first prerequisite
Names of prerequisites newer than the target
Names of all prerequisites
Name of the stem
* In case of foo.a(bar.o) $@ returns "foo.a" and $% returns "bar.o"
Certain targets can be marked as .PHONY. By doing this, you notify make that the target is not related to a specific filename. It will thus always be rebuilt.
rm *.o temp
In the example given, clean will always be rebuilt, even if a file named "clean" is found
Text manipulation functions
Syntax: $(function arguments)
Substitute substring from to to in text
Text substitutions using pattern pat in text
Same effect as patsubst, but in different form
Strip leading and trailing spaces from string
Tries to find occurence of find in strings. Returns 'find' if successful, else it returns ''
Returns words in text that match patterns
Returns words in text that DO NOT match patterns
Sort list list of strings in alfabetical order
Return the nth word in text
Return sublist of words list text starting at index s and ending at index e
Returns the number of words in text
Returns the first word in text
Returns the last word in text
General rule syntax
In general, a rule looks like this:
targets : prerequisites
or like this:
targets : prerequisites ; recipe
Recursively expanded variable
var = $(shell ls)
The expansion of $(shell ls) only happens when var is referenced
Simply expanded variable
var := $(shell ls)
var ::= $(shell ls)
The expansion of $(shell ls) is done immediately
Conditionally expanded variable
var ?= $(shell ls)
Assigns the variable recursively if it is not yet defined
var += $(shell ls)
Appends to the variable. Assignment (recursive/simple) depends on var
var != ls
Executes the ls command immediately in the shell and assigns result to var
Help Us Go Positive!
We offset our carbon usage with Ecologi. Click the link below to help us!