How to print out a variable in makefile
Solution 1:
You can print out variables as the makefile is read (assuming GNU make as you have tagged this question appropriately) using this method (with a variable named "var"):
$(info $$var is [${var}])
You can add this construct to any recipe to see what make will pass to the shell:
.PHONY: all
all: ; $(info $$var is [${var}])echo Hello world
Now, what happens here is that make stores the entire recipe ($(info $$var is [${var}])echo Hello world
) as a single recursively expanded variable. When make decides to run the recipe (for instance when you tell it to build all
), it expands the variable, and then passes each resulting line separately to the shell.
So, in painful detail:
- It expands
$(info $$var is [${var}])echo Hello world
- To do this it first expands
$(info $$var is [${var}])
-
$$
becomes literal$
-
${var}
becomes:-)
(say) - The side effect is that
$var is [:-)]
appears on standard out - The expansion of the
$(info...)
though is empty
-
- Make is left with
echo Hello world
- Make prints
echo Hello world
on stdout first to let you know what it's going to ask the shell to do
- Make prints
- The shell prints
Hello world
on stdout.
Solution 2:
As per the GNU Make manual and also pointed by 'bobbogo' in the below answer, you can use info / warning / error to display text.
$(error text…)
$(warning text…)
$(info text…)
To print variables,
$(error VAR is $(VAR))
$(warning VAR is $(VAR))
$(info VAR is $(VAR))
'error' would stop the make execution, after showing the error string