Search notes:

make: variables

Automatic variables

Some automatic variables include:
# vim: ft=make

#
#  $< represents the first prerequisite.
#

all: dep_1 dep_2 dep_3
	@echo all, $$\< = $<

dep_1: dep_1_a dep_1_b dep_1_c
	@echo dep_1, $$\< = $<

dep_2: dep_2_y dep_2_z
	@echo dep_2, $$\< = $<

dep_1_a:
	@echo dep_1_a, $$\< = $<

dep_1_b:
	@echo dep_1_b, $$\< = $<

dep_1_c:
	@echo dep_1_c, $$\< = $<

dep_2_y:
	@echo dep_2_y, $$\< = $<

dep_2_z:
	@echo dep_2_z, $$\< = $<

dep_3:
	@echo dep_3, $$\< = $<
Github repository about-Makefile, path: /variables/automatic/lt
# vim: ft=make

#
#  $@ represents the target
#

all: dep_1 dep_2
	@echo all, $$\@ = $@

dep_1: dep_1_a dep_1_b
	@echo dep_1, $$\@ = $@

dep_2: dep_2_y dep_2_z
	@echo dep_2, $$\@ = $@

dep_1_a:
	@echo dep_1_a, $$\@ = $@

dep_1_b:
	@echo dep_1_b, $$\@ = $@

dep_2_y:
	@echo dep_2_y, $$\@ = $@

dep_2_z:
	@echo dep_2_z, $$\@ = $@
Github repository about-Makefile, path: /variables/automatic/at

Assigning values to variables

There are two possible assignment operators in a Makefile: := and =.
:= evaluates the right hand side immediately and assigns the value to the left hand side.
= stores the right hand side. The evaluation takes place when the left hand side variable is evaluated.
#
#  make -f assign
#  make -f assign VAL_THREE=something
#

VARIABLE = now

VAL_ONE     = ${VARIABLE}
VAL_TWO    := ${VARIABLE}

# Only assign to VAL_THREE if VAL_THREE is not yet
# initialized
VAL_THREE  ?= Foo

VARIABLE = later

$(info )

$(info $$VAL_ONE=$(VAL_ONE))     # VAL_ONE=later
$(info $$VAL_TWO=$(VAL_TWO))     # VAL_TWO=now
$(info $$VAL_THREE=$(VAL_THREE)) #

.phony run:
run:
	@true
Github repository about-Makefile, path: /variables/assign

Star

A star is a wildcard that expands to the names in a directory:
files_in_directory = *

all:
	@echo files in directory: $(files_in_directory)
Compare with $(wildcard …).

Predefined

Some predefined variables that can be used in a makefile are:
.PHONY: all

print_var=printf "%-12s = %s\n" $1 "$($1)"

all:
	@$(call print_var,AR)
	@$(call print_var,ARFLAGS)
	@$(call print_var,AS)
	@$(call print_var,CC)
	@$(call print_var,CO)
	@$(call print_var,CURDIR)
	@$(call print_var,FC)
	@$(call print_var,LD)
	@$(call print_var,MAKE_HOST)
	@$(call print_var,MAKE_COMMAND)
	@$(call print_var,MAKE_VERSION)
	@$(call print_var,MAKEFLAGS)
	@$(call print_var,RM)
	@$(call print_var,SHELL)
	@$(call print_var,.SHELLFLAGS)
Github repository about-Makefile, path: /variables/predefined.mak

Appending values to variables

VAR+=Foo
VAR+=Bar
VAR+=Baz

$(info )
$(info $$VAR=$(VAR))

all:
	@true
Github repository about-Makefile, path: /variables/append

See also

make

Index