单词功能无法正确计算单词数 [英] words function fails to count correctly the number of words
问题描述
通过文档:
'$(words TEXT)'
返回TEXT中的单词数.因此,TEXT的最后一句话 是'$(word $(words TEXT),TEXT)'.
在实践中,对于Makefile:
define collection
foo
bar
baz
endef
number := $(words $(collection))
collection:
echo '$(number) words in my collection'
运行,我得到:
echo '1 words in my collection'
1 words in my collection
我想了一会儿,我们在那里有3个字.我错了吗?
这里的问题是,您使用了多行而没有用\
明确结束它们以告诉make
它们都是同一行的一部分.如果要使用
define collection
foo bar baz
endef
定义您的collection
define collection
foo bar baz
endef
或
define collecton
foo \
bar \
baz
endef
然后,配方collection
将正确输出3 words in my collection
.您可能还想在echo
命令之前添加@
,因为这将禁止输出echo '1 words in my collection'
MadScientist 是正确的.正是额外的空间使此正常工作.但是,我要补充的是,如果您要添加像
print:
@echo $collection
并为您提供正确的空格,除非您使用我上面提供的2种解决方案之一,单行声明或使用\
,否则每个单词之后都将以make: bar: Command not found
失败.
From the docs:
'$(words TEXT)'
Returns the number of words in TEXT. Thus, the last word of TEXT is '$(word $(words TEXT),TEXT)'.
In practice, for the Makefile:
define collection
foo
bar
baz
endef
number := $(words $(collection))
collection:
echo '$(number) words in my collection'
And running, I get:
echo '1 words in my collection'
1 words in my collection
And I, for a minute, thought that we had there 3 words. Am I wrong?
The issue here is that you have used multiple lines without explicitly ending them with \
to tell make
that they are all part of the same line. If you were to define your collection
with
define collection
foo bar baz
endef
or
define collecton
foo \
bar \
baz
endef
Then the recipe collection
would correctly output 3 words in my collection
. You may also want to add @
before the echo
command as this will suppress the output of the line echo '1 words in my collection'
[1]. @
is commonly used before lines in makefile
s when you don't want the specific commands make is running to be output. A makefile
which produces your expected output with the @
symbol is provided below for you.
define collection
foo bar baz
endef
number := $(words $(collection))
collection:
@echo '$(number) words in my collection'
EDIT: @MadScientist is correct. It is the additional space that makes this work correctly. However, I would add to this that if you were to add a recipe like
print:
@echo $collection
and providing you correctly appended a space after each word it would still fail with make: bar: Command not found
unless you use either of the 2 solutions I've provided above, the single line declaration or using \
.
这篇关于单词功能无法正确计算单词数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!