Makefile and variable target from command line

雨燕双飞 提交于 2020-01-06 12:27:47

问题


Newbie question for Makefiles... why doesn't this work?

TARGET=$@

$(TARGET): * **/*
    @echo "TARGET=$(TARGET)"

Where this does?

TARGET=my_target

$(TARGET): * **/*
    @echo "TARGET=$(TARGET)"

When started with make my_target?

Result of the former is, "no rule to make target `my_target'."

In addition to the question "why this doesn't work," is there a workaround? I'd like to be able to specify an arbitrary target from the command line. I suppose I could react to an env var, but that makes the CLI clunky, e.g., make target=my_target build or similar.

I've searched, but I'm not getting the right hits to solve this. GNU make 3.81. Thanks!


回答1:


The automatic variable $@ is defined in the context of a pattern rule; outside of any rule it has no value.

If you want Make to do the same thing to whatever target you name, you can use a match-anything rule:

%:
    @echo TARGET=$@


来源:https://stackoverflow.com/questions/50751346/makefile-and-variable-target-from-command-line

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