TExcelTAD
Home › GNU Make & Ninja › Automatic Variables

Automatic Variables

Writing cc -c geometry.c -o geometry.o repeats information that is already in the rule line geometry.o: geometry.c. Automatic variables let a recipe refer to the rule's own target and prerequisites, so one recipe text works for many rules. They are set fresh for each rule while its recipe runs (they are empty at the top level of a Makefile).

Variable Value
$@ the target being built
lt;
the first prerequisite
$^ all prerequisites, duplicates removed (order-only ones excluded)
$+ all prerequisites, duplicates kept (useful for link order with repeated libraries)
$? the prerequisites that are newer than the target
$| the order-only prerequisites
$* the stem: what % matched in a pattern rule
$(@D), $(@F) directory part and file part of $@ (also $(<D), $(<F), …)

Seeing them

The quickest way to learn them is to print them. This rule never creates its target (the recipe only echoes), so it runs every time:

bash · try it yourself
mkdir -p auto/out && cd auto
touch a.c b.h c.h
cat > Makefile <<'MK'
out/app.o: a.c b.h c.h b.h | out
	@echo '$@     = $@'
	@echo '$<     = 
lt;' @echo '$^ = $^' @echo '$+ = $+' @echo '$? = $?' @echo '$| = $|' @echo '$(@D) = $(@D)' @echo '$(@F) = $(@F)' MK make -j1
$@     = out/app.o
lt; = a.c $^ = a.c b.h c.h $+ = a.c b.h c.h b.h $? = a.c b.h c.h $| = out $(@D) = out $(@F) = app.o

$? lists everything here because the target doesn't exist. Single quotes keep the shell from touching the $ signs that $ produced.

Rewriting the rect Makefile

With automatic variables, the compile rules become identical apart from their first line, which is exactly what pattern rules will exploit next:

bash · try it yourself
mkdir -p rect && cd rect
printf 'int area(int w, int h);\n' > geometry.h
printf '#include "geometry.h"\nint area(int w, int h) { return w * h; }\n' > geometry.c
printf '#include <stdio.h>\n#include "geometry.h"\nint main(void) { printf("area = %%d\\n", area(6, 7)); return 0; }\n' > main.c
cat > Makefile <<'MK'
CC := gcc
CFLAGS := -Wall -O2

rect: main.o geometry.o
	$(CC) $^ -o $@

main.o: main.c geometry.h
	$(CC) $(CFLAGS) -c 
lt; -o $@ geometry.o: geometry.c geometry.h $(CC) $(CFLAGS) -c
lt; -o $@ MK make -j1 ./rect
gcc -Wall -O2 -c main.c -o main.o
gcc -Wall -O2 -c geometry.c -o geometry.o
gcc main.o geometry.o -o rect
area = 42

Note why

lt; and not $^ in the compile rules: $^ would pass the header to the compiler too. Listing the source file first in the prerequisites is a convention that
lt;
relies on.

$? for incremental work

$? is useful when the recipe can process only the changed inputs, as with an archive. ar r replaces just the members you give it:

bash · try it yourself
mkdir -p lib && cd lib
for n in one two three; do printf 'int %s(void) { return 1; }\n' $n > $n.c; done
cat > Makefile <<'MK'
libnums.a: one.o two.o three.o
	ar rc $@ $?
	@echo "updated members: $?"

%.o: %.c
	gcc -c 
lt; -o $@ MK make -j1 sleep 1 && touch two.c make -j1 ar t libnums.a
gcc -c one.c -o one.o
gcc -c two.c -o two.o
gcc -c three.c -o three.o
ar rc libnums.a one.o two.o three.o
updated members: one.o two.o three.o
gcc -c two.c -o two.o
ar rc libnums.a two.o
updated members: two.o
one.o
two.o
three.o

The second run rebuilt one object and replaced one member. (Production Makefiles usually rebuild the whole archive with $^ after rm -f $@, so deleted sources don't linger inside it.)

Test yourself

In app.o: app.c app.h config.h, what is

lt;?

  1. app.o
  2. app.c
  3. app.c app.h config.h
  4. config.h

Why do compile recipes use

lt; rather than $^?

  1. $^ is slower
  2. $^ would also pass the header prerequisites to the compiler
  3. lt;
    includes order-only prerequisites
  4. There is no difference

Which automatic variable lists only the prerequisites newer than the target?

  1. $^
  2. $+
  3. $?
  4. $*