When defining a recursively-expanded variable, the contents of the right-hand side are stored as-is. If a variable reference is present, the reference itself is stored (not the value of the variable). Make waits to expand the variable references until the variable is actually used.
x = hello
y = $(x)
# Both $(x) and $(y) will now yield "hello"
x = world
# Both $(x) and $(y) will now yield "world"
In this example, the definition of y
is recursive. The reference to $(x)
doesn't get expanded until $(y)
is expanded. This means that whenever the value of x
changes, the value of y
will change as well.
Recursively-expanded variables are a powerful but easily-misunderstood tool. They can be used to create constructs that resemble templates or functions, or even to automatically generate portions of a makefile. They can also be the source of hard-to-debug problems. Be careful to only use recursively-expanded variables when necessary.