target .. : prerequisite .. recipe ..
target: an output generated by the rule
prerequisite: an input that is used to generate the target
recipe: list of actions to generate the output from the input
make -pto print all rules and variables (implicitly + explicitly defined).
A pattern rule contains the
% char (exactly one of them) and look like this example:
%.o : %.c $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
The target matches files of the pattern
% matches any none-empty
substring and other character match just them self.
The substring matched by
% is called the
% in the prerequisite stands for the matched
stem in the target.
As targets and prerequisites in pattern rules can't be spelled explicitly in the recipe, make provides a set of automatic variables to work with:
$@: Name of the target that triggered the rule.
$<: Name of the first prerequisite.
$^: Names of all prerequisites (without duplicates).
$+: Names of all prerequisites (with duplicates).
$*: Stem of the pattern rule.
# file: Makefile all: foobar blabla foo% bla%: aaa bbb bbb @echo "@ = $@" @echo "< = $<" @echo "^ = $^" @echo "+ = $+" @echo "* = $*" @echo "----" aaa: bbb:
@ = foobar < = aaa ^ = aaa bbb + = aaa bbb bbb * = bar ---- @ = blabla < = aaa ^ = aaa bbb + = aaa bbb bbb * = bla ----
Variables related to filesystem paths:
$(CURDIR): Path of current working dir after using
make -C path
Arguments specified on the command line override ordinary variable assignments in the makefile (overriding variables).
VAR = abc all: @echo VAR=$(VAR)
# make VAR=abc # make VAR=123 VAR=123
Substitute strings matching pattern in a list.
in := a.o l.a c.o out := $(in:.o=.c) # => out = a.c l.a c.c
in := a.c b.c out := $(patsubst %.c, build/%.o, $(in)) # => out = build/a.o build/b.o # This is actually equivalent to $(in:%.c=build/%.o)
Keep strings matching a pattern in a list.
in := a.a b.b c.c d.d out := $(filter %.b %.c, $(in)) # => out = b.b c.c
Remove strings matching a pattern from a list.
in := a.a b.b c.c d.d out := $(filter-out %.b %.c, $(in)) # => out = a.a d.d
Resolve each file name as absolute path (don't resolve symlinks).
$(abspath fname1 fname2 ..) ### `realpath` Resolve each file name as canonical path. ```make $(realpath fname1 fname2 ..)