使用 GNU Make 将多行变量输出到文件

发布于 2024-12-03 03:55:37 字数 325 浏览 0 评论 0原文

我很难编写在文件中输出多行变量的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

总以为 2024-12-10 03:55:37

如果将变量导出到 shell 并将其作为 shell 变量而不是 make 变量引用,那么您的运气会更好:

define VAR1
    /dev d 755 - - - - -
endef

define VAR2
    /test d 777 - - - - -
    /test2 d 777 - - - - -
endef

define VARS
$(VAR1)
$(VAR2)
endef
export VARS

all:
    echo "$VARS" > test

请注意对 makefile 的以下调整:

  • 我使用 define 来创建 VARS,而不是一系列 += 赋值,这样可以更轻松地在 VAR1VAR2.
  • 我将 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:

define VAR1
    /dev d 755 - - - - -
endef

define VAR2
    /test d 777 - - - - -
    /test2 d 777 - - - - -
endef

define VARS
$(VAR1)
$(VAR2)
endef
export VARS

all:
    echo "$VARS" > test

Note the following adjustments to your makefile:

  • I used define to create VARS, rather than a series of += assignments, which makes it easier to get a newline between the value of VAR1 and VAR2.
  • I added export VARS to your makefile, to get the variable pushed into the environment for the shell invocation.
  • I used $$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.
蒲公英的约定 2024-12-10 03:55:37

直接写入文件的功能:

define VAR1
    /dev d 755 - - - - -
endef

define VAR2
    /test d 777 - - - - -
    /test2 d 777 - - - - -
endef

define VARS :=
$(VAR1)
$(VAR2)
endef

all:
        $(file > test,$(VARS))

GNU make 4.0 添加了 您仍然需要使用 define 来定义 VARS,或者 VAR1 的最后一行和 VAR2 的第一行> 将被粘在一个上 线。另外,不要在 $(file ...) 构造中的逗号后面添加空格,否则输出中将会有一个前导空格!

GNU make 4.0 adds the ability to write files directly:

define VAR1
    /dev d 755 - - - - -
endef

define VAR2
    /test d 777 - - - - -
    /test2 d 777 - - - - -
endef

define VARS :=
$(VAR1)
$(VAR2)
endef

all:
        $(file > test,$(VARS))

Note that you still need to use define to define VARS, or the last line of VAR1 and the first line of VAR2 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!

挥剑断情 2024-12-10 03:55:37

看起来好像您收到“未终止的引号字符串”,因为 Make 在单独的 shell 中执行配方的每一行,第一行是:

echo "    /dev d 755 - - - - -

这是我能想到的最佳解决方案(我承认这不是很好,但你违背了 Make 的原则)是将 VARS 传递给调用 $(info ...) 的子 Make:

Makefile:

define VAR1
    /dev d 755 - - - - -
endef

define VAR2
    /test d 777 - - - - -
    /test2 d 777 - - - - -
endef

define VARS
$(VAR1)
$(VAR2)
endef

export VARS

all:
       $(MAKE) -f Makefile.print > test                                        

Makefile.print:

$(info $(VARS))

.PHONY:all
all:
        @# do nothing

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:

echo "    /dev d 755 - - - - -

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:

define VAR1
    /dev d 755 - - - - -
endef

define VAR2
    /test d 777 - - - - -
    /test2 d 777 - - - - -
endef

define VARS
$(VAR1)
$(VAR2)
endef

export VARS

all:
       $(MAKE) -f Makefile.print > test                                        

Makefile.print:

$(info $(VARS))

.PHONY:all
all:
        @# do nothing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文