单词功能无法正确计算单词数 [英] words function fails to count correctly the number of words

查看:94
本文介绍了单词功能无法正确计算单词数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过文档:

'$(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 makefiles 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆