How to prevent GNU Make from executing when a variable definition has trailing spaces

故事扮演 提交于 2019-12-24 10:20:12

问题


I had a simple variable definition in my makefile:

THIS         := ~/edan 

and it had trailing spaces in the line.

Later, when I defined another variable in terms of this one:

WARES       := $(THIS)/wares

the actual variable defined was /home/directory/edan wares

and then the make clean rule removed /home/directory/edan instead of what I wanted it to.

How can I prevent a makefile from executing if a line has trailing spaces?


回答1:


Although you could write the Makefile so that it checks the variable for trailing whitespace and exits if it finds some, there are better ways of dealing with this situation.

In this instance, I would recommend using the addsuffix function to catenate $(THIS) and /wares instead of doing it manually. The addsuffix function will strip the trailing whitespace for you. In other words:

WARES := $(addsuffix /wares,$(THIS))

If you really want it to exit if it discovers trailing whitespace, you could do this:

THIS := ~/edan
ifneq "$(THIS)" "$(strip $(THIS))"
    $(error Found trailing whitespace)
endif


来源:https://stackoverflow.com/questions/4053379/how-to-prevent-gnu-make-from-executing-when-a-variable-definition-has-trailing-s

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