是否可以在 Makefile 中创建多行字符串变量 [英] Is it possible to create a multi-line string variable in a Makefile

查看:26
本文介绍了是否可以在 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 的使用,表示shell 环境变量引用,而不是$(ANNOUNCE_BODY),后者将是常规的make 变量引用.还要确保在变量引用周围使用引号,以确保换行符不会被 shell 本身解释.

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.

当然,这个特殊的技巧可能对平台和外壳敏感.我用 GNU bash 3.2.13 在 Ubuntu Linux 上测试了它;天啊.

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屋!

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