GNU make 引用

发布于 2024-11-02 16:26:04 字数 202 浏览 5 评论 0原文

引用有什么区别吗?

libmylib.a: ...
    $(AR) $(ARFLAGS) $@ $?
# vs
libmylib.a: ...
    "$(AR) $(ARFLAGS) $@ $?"


@echo Compiler: $(CXX)
# vs
@echo "Compiler: $(CXX)"

谢谢!

Does quoting makes any difference?

libmylib.a: ...
    $(AR) $(ARFLAGS) $@ $?
# vs
libmylib.a: ...
    "$(AR) $(ARFLAGS) $@ $?"


@echo Compiler: $(CXX)
# vs
@echo "Compiler: $(CXX)"

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

娇柔作态 2024-11-09 16:26:04

makefile 规则中的命令只是简单地输入到 shell,因此引用绝对重要。这个规则会起作用:

libmylib.a: ...
    $(AR) $(ARFLAGS) $@ $?

但这个不会起作用:

libmylib.a: ...
    "$(AR) $(ARFLAGS) $@ $?"

第二个不会起作用,因为你可能没有一个名为 ar -r libx.a ao bo 的命令,即使你确实有一个 ar 命令知道如何处理 -r libx.a ao bo 参数列表。

您的第二个示例:

@echo Compiler: $(CXX)
# vs
@echo "Compiler: $(CXX)"

有点不同,因为引号位于 shell 内置 echo 的参数周围,因此这两个规则:

rule1:
    @echo Compiler: $(CXX)

rule2:
    echo "Compiler: $(CXX)"

通常会产生相同的输出(当然,除非您有一些奇怪的输出)例如,名称中包含“>”的 C++ 编译器命令,在这种情况下,无论是谁命名您的 C++ 编译器,所需的输出都会令人大吃一惊,这甚至超出了 GNU make 的能力)。另一方面,由于引号和空格的相互作用,这两个规则会产生略有不同的输出:

rule3:
    @echo Compiler:           $(CXX)

rule4:
    echo "Compiler:           $(CXX)"

rule3 会执行与rule1 相同的操作,但rule4 会打印一个中间带有一堆空格的字符串。

The commands in makefile rules are simply fed to the shell so quoting absolutely does matter. This rule will work:

libmylib.a: ...
    $(AR) $(ARFLAGS) $@ $?

but this one won't:

libmylib.a: ...
    "$(AR) $(ARFLAGS) $@ $?"

The second won't work because you probably don't have a single command called, say, ar -r libx.a a.o b.o even though you do have an ar command that knows what to do with the -r libx.a a.o b.o argument list.

Your second example:

@echo Compiler: $(CXX)
# vs
@echo "Compiler: $(CXX)"

is a bit different because the quotes are around the argument to the shell built-in echo so these two rules:

rule1:
    @echo Compiler: $(CXX)

rule2:
    echo "Compiler: $(CXX)"

would usually produce the same output (unless, of course, you had some bizarre C++ compiler command that had, for example, a ">" in its name and, in this case, the desired output would be a smack upside the head to whoever named your C++ compiler and that's beyond the abilities of even GNU make). On the other hand, these two rules would produce slightly different output due to the interaction of the quotes and the whitespace:

rule3:
    @echo Compiler:           $(CXX)

rule4:
    echo "Compiler:           $(CXX)"

rule3 would do the same thing as rule1 but rule4 would print a string with a bunch of whitespace in the middle.

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