来自文档:
$(patsubst PATTERN,REPLACEMENT,TEXT)
在TEXT中查找与PATTERN匹配的以空格分隔的单词,并将其替换为REPLACEMENT.这里PATTERN可以包含一个%充当通配符的字符串,匹配单词中任意数量的任何字符.
......
单词之间的空格被折叠成单个空格字符; 前导和尾随空格被丢弃.
现在,给定一个makefile,是:
# The pattern for patsubst, does NOT contain '%'
foo := $(patsubst x,y,x x x)
# The pattern for patsubst, does contain '%'
bar := $(patsubst x%,y,x x x)
# The variable 'foo', is a result from a patsubst-pattern, that did NOT contain a '%'
# The variable 'bar', is a result from a patsubst-pattern, that did contain a '%'
all ::
@echo 'foo is: "$(foo)"' …Run Code Online (Sandbox Code Playgroud) 由于条件指令ifeq经常用于比较从变量(通常包含空格)扩展的单词,我们可能希望并且实际上需要 Make来去除任何前导或尾随空格.
事实上,你可能有一个相反的观点,即Make应该逐字记录ifeq条件的所有参数,因为用户可能已经将这些空格作为"测试"的一部分,并打算让这些空白进行决策在评估此ifeq指令时,factor 为true或false.
我无法决定,哪一个更正确.
事实上,我并不孤单!
让自己无法决定,哪一个是正确的.因此,它可能会也可能不会剥离前导或尾随空格.
实际上,有时它只会剥离前导空格.
不令人失望,Make有时会 剥离尾随空格.
当然,检查的案例太多了,所以我只会"做"其中的几个.
makefile(VERSION 1)是:
ifeq ( a, a)
all::
echo 'true'
else
all::
echo 'false'
endif
Run Code Online (Sandbox Code Playgroud)
执行,我得到:
$ make -r
echo 'false'
false
Run Code Online (Sandbox Code Playgroud)
makefile(VERSION 2)是:
ifeq (a ,a )
all::
echo 'true'
else
all::
echo 'false'
endif
Run Code Online (Sandbox Code Playgroud)
执行,我得到:
$ make -r
echo 'false'
false
Run Code Online (Sandbox Code Playgroud)
makefile(VERSION 3)是:
ifeq …Run Code Online (Sandbox Code Playgroud) 来自文档:
.ONESHELL
如果.ONESHELL被提到作为目标,那么当构建目标时,配方的所有行将被赋予shell的单个调用,而不是单独调用每行(*note Recipe Execution:Execution.).
所以,一个makefile,如:
.ONESHELL :
all ::
echo 'foo
bar'
Run Code Online (Sandbox Code Playgroud)
跑步,我得到:
$ make
echo 'foo
/bin/sh: 1: Syntax error: Unterminated quoted string
makefile:4: recipe for target 'all' failed
make: [all] Error 2 (ignored)
Run Code Online (Sandbox Code Playgroud)
尝试,几乎相同的makefile,但是如果添加-前缀配方,忽略错误,记录:
要忽略配方行中的错误,请在行的文本开头(在初始选项卡之后)写一个" - ".在将行传递给shell以执行之前,将丢弃" - ".
makefile,如:
.ONESHELL :
all ::
-echo 'foo
bar'
Run Code Online (Sandbox Code Playgroud)
跑步,我得到:
$ make
echo 'foo
/bin/sh: 1: Syntax error: Unterminated quoted string
makefile:4: recipe for target 'all' failed
make: [all] Error 2 …Run Code Online (Sandbox Code Playgroud)