抑制 make clean 中的消息(Makefile 无提示删除)

发布于 2024-09-07 06:21:53 字数 181 浏览 3 评论 0原文

我想知道如何避免 Makefile 中出现一些回声:

clean:
    rm -fr *.o

此规则将打印:

$>make clean   
rm -fr *.o

$>

我怎样才能避免这种情况?

I'm wondering how I can avoid some echo in a Makefile :

clean:
    rm -fr *.o

this rule will print:

gt;make clean   
rm -fr *.o

gt;

How can I avoid that?

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

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

发布评论

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

评论(6

变身佩奇 2024-09-14 06:21:53

首先:实际的命令必须在下一行(或者至少 GNU Make 是这样,它可能与其他 Make 不同 - 我不确定)

clean:
    rm -rf *.o

(注意,你需要一个 每条规则中的 rm -rf *.o 之前的 TAB

可以通过在 @ 前面添加前缀来使其保持沉默:

这样你的 makefile 就变成了

clean:
    @rm -rf *.o

如果没有要删除 *.o 文件,您可能仍会收到错误消息。要抑制这些,请添加以下

clean:
    -@rm -rf *.o 2>/dev/null || true
  • 2>/dev/null 将任何错误消息通过管道传输到 /dev/null - 这样您就不会
  • 在前面的 - 看到任何错误该命令确保 make 忽略非零返回代码

To start with: the actual command must be on the next line (or at least that is the case with GNU Make, it might be different with other Make's - I'm not sure of that)

clean:
    rm -rf *.o

(note, you need a TAB before rm -rf *.o as in every rule)

Making it silent can be done by prefixing a @:

so your makefile becomes

clean:
    @rm -rf *.o

If there are no *.o files to delete, you might still end up with an error message. To suppress these, add the following

clean:
    -@rm -rf *.o 2>/dev/null || true
  • 2>/dev/null pipes any error message to /dev/null - so you won't see any errors
  • the - in front of the command makes sure that make ignores a non-zero return code
冷清清 2024-09-14 06:21:53

事实上,我一直在寻找其他东西,将这一行添加到 Makefile 中:

.SILENT:clean

while 默默地执行“clean”目标的每一步。

直到有人指出这个的一些缺点,我用它作为我最喜欢的解决方案!

In fact I was looking for something else, adding this line to the Makefile :

.SILENT:clean

while execute every step of the "clean" target silently.

Until someone point some drawback to this, I use this as my favourite solution!

终弃我 2024-09-14 06:21:53

我之所以回应这个古老的话题,是因为它在搜索中的排名很高,而且答案令人困惑。为了做用户想要的,所需要的只是:

clean:
    @rm -f *.o

@ 意味着 make 不会回显该命令。
rm-f 参数告诉 rm 忽略任何错误,比如没有 *.o 文件,并永远回报成功。

我从 OP 示例中删除了 -r,因为它意味着递归,这里我们只是 rm .o 文件,没有任何可递归的内容。

不需要 2>&1 >/dev/null 因为使用 -f 不会打印任何错误。

.SILENT: clean

代替 @ 工作,但它在 Makefile 中与其影响的命令不在同一位置,因此稍后维护该项目的人可能会感到困惑。这就是为什么@是首选。这是更好的参考位置。

I'm responding to this ancient topic because it comes up high in search and the answers are confusing. To do just what the user wants,all that is needed is:

clean:
    @rm -f *.o

The @ means that make will not echo that command.
The -f argument to rm tells rm to ignore any errors, like there being no *.o files, and to return success always.

I removed the -r from the OPs example, because it means recursive and here we are just rming .o files, nothing to recurse.

There's no need for the 2>&1 >/dev/null because with the -f there will be no errors printed.

.SILENT: clean

works in place of the @, but it isn't at the same place in the Makefile as the command that it affects, so someone maintaining the project later might be confused. That's why @ is preferred. It is better locality of reference.

十秒萌定你 2024-09-14 06:21:53

如果在命令前面放置 @,它不会回显到 shell 上。尝试将 rm 更改为 @rm。 (参考

If you put an @ in front of the command, it doesn't echo onto the shell. Try changing rm to @rm. (Reference)

仄言 2024-09-14 06:21:53

从手册中: .SILENT 基本上已经过时了@ 更加灵活。

更糟糕的是 make 打印太多信息。警告/错误/私人消息隐藏在输出中。另一方面,-s (.SILENT) 会抑制任何内容。尤其是“无事可做”和“最新”消息可能会很痛苦。没有办法压制他们。您必须主动过滤掉它们或使用诸如 colormake 之类的东西。这是 grep 的解决方案:

make | egrep -hiv 'nothing to be done|up to date'

但是输出将有行号。因此,Perl 解决方案更好,因为它抑制行号并立即刷新标准输出:

make | perl -ne '$|=1; print unless /nothing to be done|up to date/i'

Make 是一个有缺陷的工具。 “GNU make 出了什么问题?” 比我更好地解释了这一点。

From the manual: .SILENT is essentially obsolete since @ is more flexible.

Much worse is that make prints far too much information. Warning/error/private messages are buried in the output. On the other hand -s (.SILENT) suppresses just anything. Especially the "nothing to be done" and "up to date" messages can be a pain. There is no option to suppress them. You have to filter them out actively or use something like colormake. Here is a solution for grep:

make | egrep -hiv 'nothing to be done|up to date'

But the output will have line numbers. The Perl solution is therefore better, because it suppresses line numbers and flushes stdout immediately:

make | perl -ne '$|=1; print unless /nothing to be done|up to date/i'

Make's a flawed tool. "What’s Wrong With GNU make?" explains this better than I can.

知你几分 2024-09-14 06:21:53

有一篇关于使用 .SILENT很棒的文章这解释了如何有条件地激活它。

我已使用该信息将其放入我的 Makefile 中:

# Use `make V=1` to print commands.
$(V).SILENT:

# Example rule, only the @echo needs to be added to existing rules
*.o: %.c
    @echo " [CC]  
lt;"
    gcc ...

它的作用是,如果您正常运行 make,正常输出将被静音,而 echo 命令会起作用:

$ make
 [CC]  test.c
 [CC]  test2.c

但它允许您可以通过传递 V=1 参数来调试问题,该参数仍然显示 [CC] 消息,因为它有助于分解输出,但传统的 Makefile 输出也是可见的:

$ make V=1
 [CC]  test.c
gcc ...
 [CC]  test2.c
gcc ...

There's a great article on using .SILENT that explains how to conditionally activate it.

I have used that information to put this in my Makefile:

# Use `make V=1` to print commands.
$(V).SILENT:

# Example rule, only the @echo needs to be added to existing rules
*.o: %.c
    @echo " [CC]  
lt;"
    gcc ...

What this does is if you run make normally, normal output is silenced and instead the echo commands work:

$ make
 [CC]  test.c
 [CC]  test2.c

But it allows you to debug problems by passing the V=1 parameter, which still shows the [CC] messages as it helps break up the output, but the traditional Makefile output is also visible:

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