使用 GNU Make 将多行变量输出到文件
我很难编写在文件中输出多行变量的 makefile 规则。
这是我的代码:
define VAR1
/dev d 755 - - - - -
endef
define VAR2
/test d 777 - - - - -
/test2 d 777 - - - - -
endef
VARS += $(VAR1)
VARS += $(VAR2)
all:
echo "$(VARS)" > test
但是,由于我未知的原因,回显无法告诉“未终止的引用字符串”。 我怎样才能将在单独的行上声明的每一行放入文件中?
I'm having hard time writing a makefile rule that outputs a multiline variable in a file.
Here is the code I have :
define VAR1
/dev d 755 - - - - -
endef
define VAR2
/test d 777 - - - - -
/test2 d 777 - - - - -
endef
VARS += $(VAR1)
VARS += $(VAR2)
all:
echo "$(VARS)" > test
However, the echo fails telling "Unterminated quoted string" for a reason unknown to me.
How could i put in the file every lines declared on a separate line ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果将变量导出到 shell 并将其作为 shell 变量而不是 make 变量引用,那么您的运气会更好:
请注意对 makefile 的以下调整:
define
来创建VARS
,而不是一系列+=
赋值,这样可以更轻松地在VAR1
和VAR2.
export VARS
添加到您的 makefile 中,以将变量推送到 shell 调用的环境中。$$VARS
而不是$(VARS)
来取消引用它——这将扩展留给 shell,而不是 make,这将避免“未终止的引用”字符串”错误。If you export the variable to the shell and reference it as a shell variable, rather than a make variable, you will have better luck:
Note the following adjustments to your makefile:
define
to createVARS
, rather than a series of+=
assignments, which makes it easier to get a newline between the value ofVAR1
andVAR2
.export VARS
to your makefile, to get the variable pushed into the environment for the shell invocation.$$VARS
rather than$(VARS)
to dereference it -- that leaves the expansion to the shell, rather than to make, which will avoid the "Unterminated quoted string" error.直接写入文件的功能:
GNU make 4.0 添加了 您仍然需要使用
define
来定义VARS
,或者VAR1
的最后一行和VAR2
的第一行> 将被粘在一个上 线。另外,不要在$(file ...)
构造中的逗号后面添加空格,否则输出中将会有一个前导空格!GNU make 4.0 adds the ability to write files directly:
Note that you still need to use
define
to defineVARS
, or the last line ofVAR1
and the first line ofVAR2
will be glommed onto one line. Also, don't put a space after the comma in the$(file ...)
construct, or there will be a leading space in the output!看起来好像您收到“未终止的引号字符串”,因为 Make 在单独的 shell 中执行配方的每一行,第一行是:
这是我能想到的最佳解决方案(我承认这不是很好,但你违背了 Make 的原则)是将 VARS 传递给调用
$(info ...)
的子 Make:Makefile:
Makefile.print:
It looks as if you're getting "Unterminated quoted string" because Make executes each line of the recipe in a separate shell, and the first line is:
Here's the best solution I could come up with (and I admit it's not very good, but you're going against the grain of Make) is to pass
VARS
to a sub-Make which invokes$(info ...)
:Makefile:
Makefile.print: