问题
I was running the following commands in a target in my Makefile.
target1:
<run some command which creates ids.log>
$(eval id1 := $(shell cat ids.log | head -1))
@echo id is $(id1)
<some other command which uses the value $(id1)>
My task is to get the first line in "ids.log" and keep it in a variable id1, so that I can use the value in the command I will execute next in this target.
I get an error running this command "cat: ids.log: No such file or directory". Looks like $(eval .. ) command is executed at the beginning of make rather than as part of the target make. And since the ids.log is not created until the target make is done, we get this error.
Can anyone help in how to get the value of the shell command I execute and put it in a variable for use on demand?
回答1:
Have a separate target that makes ids.log
first then have that as a dependency to target
.
for example:
ids.log:
echo 1 > ids.log
target: ids.log
$(eval id1 := $(shell cat ids.log | head -1))
@echo id is $(id1)
来源:https://stackoverflow.com/questions/29534798/makefile-variable-assignment-under-a-target