Show Menu

GNUMake Cheat Sheet by

Cheat sheet with commonly used constructions in Makefiles based on GNU Make
make     makefile     gnu     beginner

Make command line options

target
Select the target to run
-f file
Select which file to read
-W file
Mark file as 'out of date'
-C
Change directory before making
-d
Print all debug inform­ation
-n
Print actions without making
-t
Mark encoun­tered targets as 'up of date'
-p
Expand makefile and print
-b
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"

.PHONY

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
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: $(fun­ction argume­nts)
$(su­bst from­,t­otext)
 
Substitute substring from to to in text
$(pa­tsu­bst patre­pl­,t­ext)
 
Text substi­tutions using pattern pat in text
$(tex­tpa­trepl)
 
Same effect as patsubst, but in different form
$(st­rip string)
 
Strip leading and trailing spaces from string
$(fi­nds­tring find­,s­tri­ngs)
 
Tries to find occurence of find in strings. Returns 'find' if succes­sful, else it returns ''
$(fi­lter patte­rns­,­text)
 
Returns words in text that match patterns
$(fi­lte­r-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,t­ext)
 
Return the nth word in text
$(wo­rdl­ist s,,­text)
 
Return sublist of words list text starting at index s and ending at index e
$(wo­rds text)
 
Returns the number of words in text
$(fi­rst­word text)
 
Returns the first word in text
$(la­stw­ord text)
 
Returns the last word in text
 

General rule syntax

In general, a rule looks like this:
targ­ets : prere­qui­sites
recipe
...
or like this:
targ­ets : prereq­uisites ; recipe
recipe
...

Variable assignment

Recu­rsively 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

Cond­iti­onally expanded variable
 ­ var ?= $(shell ls)
Assigns the variable recurs­ively if it is not yet defined

Incr­emental assign­ment
 ­ var += $(shell ls)
Appends to the variable. Assignment (recur­siv­e/s­imple) depends on var

Shell assign­ment
 ­ var != ls
Executes the ls command immedi­ately in the shell and assigns result to var

Download the GNUMake Cheat Sheet

1 Page
//media.cheatography.com/storage/thumb/bavo-van-achte_gnumake.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

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