makefile variable assignment under a target

假装没事ソ 提交于 2019-12-14 03:46:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!