Show Menu

Cheat sheet with commonly used constructions in Makefiles based on GNU Make

Make command line options

Select the target to run
-f file
Select which file to read
-W file
Mark file as 'out of date'
Change directory before making
Print all debug inform­ation
Print actions without making
Mark encoun­tered targets as 'up of date'
Expand makefile and print
Consider all targets as 'out-o­f-date'

Automatic variables

Name of the target of the recipe being run*
The target member name, when the target is an archive member*
Name of first prereq­uisite
Names of prereq­uisites newer than the target
Names of all prereq­uisites
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.

.PHONY: clean
 ­ ­ ­ rm *.o temp
In the example given, clean will always be rebuilt, even if a file named "­cle­an" is found

Text manipu­lation functions

Syntax: $(function arguments)
$(subst from,to,text)
Substitute substring from to to in text
$(patsubst pat,repl,text)
Text substi­tutions using pattern pat in text
Same effect as patsubst, but in different form
$(strip string)
Strip leading and trailing spaces from string
$(findstring find,strings)
Tries to find occurence of find in strings. Returns 'find' if succes­sful, else it returns ''
$(filter patterns,text)
Returns words in text that match patterns
$(filter-out patter­n...,text)
Returns words in text that DO NOT match patterns
$(sort list)
Sort list list of strings in alfabe­tical order
$(word n,text)
Return the nth word in text
$(wordlist s,e,text)
Return sublist of words list text starting at index s and ending at index e
$(words text)
Returns the number of words in text
$(firstword text)
Returns the first word in text
$(lastword text)
Returns the last word in text

General rule syntax

In general, a rule looks like this:
targets : prereq­uisites
or like this:
targets : prereq­uisites ; recipe

Variable assignment

Recurs­ively 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 immedi­ately

Condit­ionally expanded variable
var ?= $(shell ls)

Assigns the variable recurs­ively if it is not yet defined

Increm­ental assignment
var += $(shell ls)

Appends to the variable. Assignment (recur­siv­e/s­imple) depends on var

Shell assignment
var != ls

Executes the ls command immedi­ately in the shell and assigns result to var


Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          EQ tips Cheat Sheet
          AngularJS Cheat Sheet

          More Cheat Sheets by bavo.van.achte

          Total Commander Keyboard Shortcuts