Reputation:
how to create local variable in a Makefile recipe in which only it is assignable and also just used there only ?
Upvotes: 1
Views: 410
Reputation: 96236
You can use so-called target-specific variables:
foo: bar = 1
foo:
@echo $(bar)
Or, if they are not flexible enough for you, consider using eval
.
eval
creates/manipulates global variables, so to avoid name collisions, I'd use a prefix for the variables you create with it:
foo:
$(eval _local_bar = 1)
@echo $(_local_bar)
Upvotes: 2
Reputation: 52284
The recipe is executed by the shell, usually line by line. So you'll have to use your shell definition of variables, use escape if your shell uses $
for variables, and use escapes so that the recipe is logically one line. For instance on Unix, you can do:
target: <dependencies>
v=42 ; \
echo $$v
Upvotes: 2