gpt4 book ai didi

makefile - Makefile 变量中的尾随空格

转载 作者:行者123 更新时间:2023-12-04 05:59:21 33 4
gpt4 key购买 nike

生成文件:

#there is a whitespace after "/my/path/to"
FOO = "/my/path/to"
BAR = "dir"

INCLUDE_DIRS = $(FOO)/$(BAR) "/another/path"

INCLUDES = $(foreach dir,$(INCLUDE_DIRS),-I$(dir))

all:
@echo $(INCLUDES)

使用 Gnu make 我希望我的 $(INCLUDES) 是:
-I/my/path/to/dir -I/another/path

但是,如果该行
FOO = "/my/path/to"

以空格结尾(这是一个常见的“错误”),变量 FOO 将包含空格,结果 INCLUDES 将包含三个目录(两个第一个被拆分的第一个):
-I/my/path/to -I/dir -I/another/path

我找到的唯一解决方案是使用 strip 功能:
FOO = $(strip "/my/path/to" )

但是没有更自然的语法,或者有什么方法可以避免这个陷阱?

最佳答案

首先,请注意路径周围可能不应该有双引号。在你的例子中,我猜 $(FOO)/$(BAR) 的值将是 "/my/path/to"/"dir"而不是预期的 /my/path/to/dir .

回答你的问题,一般来说,没有。连接两个值会保留空格,所以如果你想写 $(FOO)/$(BAR)由您来保证 $(FOO)$(BAR)是没有前导或尾随空格的单个单词。 strip功能足以删除后者(如果有)。

但是,您可以将这些变量之一视为列表并编写类似 $(FOO:%=%/$(BAR)) 的内容。这将正常工作。但我个人更愿意检查 FOO 的值(修复它或如果它不好则失败)然后像往常一样使用它,例如:

FOO = /my/path/to # <- a space!
BAR = dir

...

ifneq ($(word 2,[$(FOO)]),)
$(error There is a whitespace inside the value of 'FOO')
endif

关于makefile - Makefile 变量中的尾随空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9116283/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com