Reputation: 3
I have a simple Makefile where I would like to set a variable in one of the targets (so that I can use this variable in other targets).
My simple Makefile:
VAR=DEFAULT
import:
echo $@
VAR=$@
echo $(VAR)
Output when I run "make import":
echo import
import
VAR=import
echo DEFAULT
DEFAULT
I was expecting VAR to be set to "import" but what's going on? Am I missing something here? Any help is appreciated! Thanks.
PS: Sorry if the question feels silly, tried googling a bit, don't have much time to do RnD! :-|
Upvotes: 0
Views: 656
Reputation: 5341
You cannot reassign the value to a variable which is already assigned with a value in the rules.
In your case: VAR=$@
will not reassign the value import
to the VAR
as it is done inside the rules list.
For this to work, you will have to do the same as part the dependency list, as below.
VAR=DEFAULT
#here VAR is assigned with the value import
import: VAR=$@
import:
echo $@
echo $(VAR)
Upvotes: 1