Make will invoke every logical line of a recipe in a separate shell. So for something like:
foo:
echo line1
echo line2
this starts two different shells, one to run the first echo and another to run the second (in reality make has some performance enhancements so that it avoids starting a shell if the command is "simple enough", but the behavior is the same either way).
So if you write a variable in one line then use it in another line, the value will be lost when the shell exits:
foo:
foo=bar
echo $$foo
prints nothing. In order to use multiple physical lines but a single logical line you must end each physical line with a backslash/newline combination: make will collect them and send them to a single shell:
foo:
foo=bar; \
echo $$foo
will print "bar".
This holds true even within multi-line variable assignments. Also, you must use $$
(as I do above) when you want to refer to a shell variable. Using a single $
will refer to the make variable by that name. And the shell variable referencing doesn't support parentheses (but you can use curly braces if you want). So you must write:
define my_function
my_var="Hello"; \
echo $$my_var; \
echo Recvd arg = $(1);
endef