是否可以在Makefile中创建多行字符串变量 [英] Is it possible to create a multi-line string variable in a Makefile
问题描述
我想创建一个makefile变量,该变量是多行字符串(例如,电子邮件发布公告的正文).像
I want to create a makefile variable that is a multi-line string (e.g. the body of an email release announcement). something like
ANNOUNCE_BODY="
Version $(VERSION) of $(PACKAGE_NAME) has been released
It can be downloaded from $(DOWNLOAD_URL)
etc, etc"
但是我似乎找不到解决此问题的方法.有可能吗?
But I can't seem to find a way to do this. Is it possible?
推荐答案
是的,您可以使用define关键字声明多行变量,如下所示:
Yes, you can use the define keyword to declare a multi-line variable, like this:
define ANNOUNCE_BODY
Version $(VERSION) of $(PACKAGE_NAME) has been released.
It can be downloaded from $(DOWNLOAD_URL).
etc, etc.
endef
棘手的部分是将多行变量从Makefile中撤回.如果您只是使用"echo $(ANNOUNCE_BODY)"做显而易见的事情,那么您会看到其他人在此处发布的结果-shell尝试将变量的第二行和后续行作为命令本身处理.
The tricky part is getting your multi-line variable back out of the makefile. If you just do the obvious thing of using "echo $(ANNOUNCE_BODY)", you'll see the result that others have posted here -- the shell tries to handle the second and subsequent lines of the variable as commands themselves.
但是,您可以按原样将变量值作为环境变量导出到Shell,然后从Shell作为环境变量引用它(而不是make变量).例如:
However, you can export the variable value as-is to the shell as an environment variable, and then reference it from the shell as an environment variable (NOT a make variable). For example:
export ANNOUNCE_BODY
all:
@echo "$$ANNOUNCE_BODY"
请注意使用$$ANNOUNCE_BODY
表示外壳环境变量引用,而不是$(ANNOUNCE_BODY)
,这将是常规的make变量引用.另外,请确保在变量引用周围使用引号,以确保换行符不会被外壳程序本身解释.
Note the use of $$ANNOUNCE_BODY
, indicating a shell environment variable reference, rather than $(ANNOUNCE_BODY)
, which would be a regular make variable reference. Also be sure to use quotes around your variable reference, to make sure that the newlines aren't interpreted by the shell itself.
当然,此技巧可能对平台和外壳敏感.我在Ubuntu Linux上使用GNU bash 3.2.13对其进行了测试; YMMV.
Of course, this particular trick may be platform and shell sensitive. I tested it on Ubuntu Linux with GNU bash 3.2.13; YMMV.
这篇关于是否可以在Makefile中创建多行字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!