抑制 make clean 中的消息(Makefile 无提示删除)
我想知道如何避免 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先:实际的命令必须在下一行(或者至少 GNU Make 是这样,它可能与其他 Make 不同 - 我不确定)
(注意,你需要一个 每条规则中的
rm -rf *.o
之前的 TAB)可以通过在
@
前面添加前缀来使其保持沉默:这样你的 makefile 就变成了
如果没有要删除
*.o
文件,您可能仍会收到错误消息。要抑制这些,请添加以下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)
(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
If there are no
*.o
files to delete, you might still end up with an error message. To suppress these, add the following2>/dev/null
pipes any error message to /dev/null - so you won't see any errors-
in front of the command makes sure thatmake
ignores a non-zero return code事实上,我一直在寻找其他东西,将这一行添加到 Makefile 中:
while 默默地执行“clean”目标的每一步。
直到有人指出这个的一些缺点,我用它作为我最喜欢的解决方案!
In fact I was looking for something else, adding this line to the Makefile :
while execute every step of the "clean" target silently.
Until someone point some drawback to this, I use this as my favourite solution!
我之所以回应这个古老的话题,是因为它在搜索中的排名很高,而且答案令人困惑。为了做用户想要的,所需要的只是:
@ 意味着 make 不会回显该命令。
rm
的-f
参数告诉rm
忽略任何错误,比如没有*.o
文件,并永远回报成功。我从 OP 示例中删除了 -r,因为它意味着递归,这里我们只是 rm .o 文件,没有任何可递归的内容。
不需要
2>&1 >/dev/null
因为使用-f
不会打印任何错误。代替
@
工作,但它在 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:
The @ means that make will not echo that command.
The
-f
argument torm
tellsrm
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
rm
ing.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.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.如果在命令前面放置 @,它不会回显到 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)
从手册中:
.SILENT
基本上已经过时了@
更加灵活。更糟糕的是 make 打印太多信息。警告/错误/私人消息隐藏在输出中。另一方面,
-s
(.SILENT
) 会抑制任何内容。尤其是“无事可做”和“最新”消息可能会很痛苦。没有办法压制他们。您必须主动过滤掉它们或使用诸如 colormake 之类的东西。这是 grep 的解决方案:但是输出将有行号。因此,Perl 解决方案更好,因为它抑制行号并立即刷新标准输出:
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:But the output will have line numbers. The Perl solution is therefore better, because it suppresses line numbers and flushes stdout immediately:
Make's a flawed tool. "What’s Wrong With GNU make?" explains this better than I can.
有一篇关于使用
.SILENT
的很棒的文章这解释了如何有条件地激活它。我已使用该信息将其放入我的 Makefile 中:
它的作用是,如果您正常运行
make
,正常输出将被静音,而echo
命令会起作用:但它允许您可以通过传递
V=1
参数来调试问题,该参数仍然显示[CC]
消息,因为它有助于分解输出,但传统的 Makefile 输出也是可见的: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:
What this does is if you run
make
normally, normal output is silenced and instead theecho
commands work: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: