当变量定义有尾随空格时,如何阻止GNU Make执行 [英] How to prevent GNU Make from executing when a variable definition has trailing spaces
问题描述
我的makefile文件中有一个简单的变量定义:
THIS := ~/edan
并且该行中有尾随空格.
后来,当我根据此变量定义另一个变量时:
WARES := $(THIS)/wares
定义的实际变量为/home/directory/edan wares
,然后make clean
规则删除了/home/directory/edan
,而不是我想要的.
如果一行中有尾随空格,如何阻止makefile执行?
尽管您可以编写Makefile,以便它检查变量的尾部空白并在发现变量时退出,但是有更好的方法来处理这种情况. /p>
在这种情况下,我建议使用addsuffix
函数对$(THIS)
和/wares
进行分类,而不是手动进行. addsuffix
函数将为您去除尾随空格.换句话说:
WARES := $(addsuffix /wares,$(THIS))
如果您确实希望它在发现尾随空白时退出,则可以执行以下操作:
THIS := ~/edan
ifneq "$(THIS)" "$(strip $(THIS))"
$(error Found trailing whitespace)
endif
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?
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
这篇关于当变量定义有尾随空格时,如何阻止GNU Make执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!