仅禁止显示实际的 make 命令和最终打印语句

Suppress only the actual make command and final print statement

本文关键字:打印 语句 命令 make 禁止显示      更新时间:2023-10-16

我有一个程序,可以将东西 - 数字 - 输出到stdout。

该程序在自动评分器中与make run input.txt 0.87 > output_file一起运行(这是针对作业的)。程序必须以这种方式运行(即使它实际上不应该用于 make 文件)。

但是,我在这里的一个 SO 问题的帮助下让它工作。这是我的制作文件:

pr.exe : main.cpp
g++ -std=c++11 $^ -o $@
run:
@./pr.exe $(filter-out $@,$(MAKECMDGOALS))

%:
@:
compile : pr.exe
.PHONY : compile

只有一个问题:命令make run input.txt 0.87输出,除了程序输出之外,最后一行:

make: 'input.txt' is up to date.

如何在 make 文件中取消此行,以便仅将程序输出重定向到文件中?

它必须使用以下命令正常运行(没有 make 参数,nada):

make run input.txt 0.87 > some_output_file.txt

message的打印被-smake选项禁用:

https://github.com/mturk/gnumake/blob/9c3fecf4dd76a8c802055d9bd3689d0c6c5d6167/remake.c#L229

if (!rebuilding_makefiles
/* If the update_status is zero, we updated successfully
or not at all.  G->changed will have been set above if
any commands were actually started for this goal.  */
&& file->update_status == 0 && !g->changed
/* Never give a message under -s or -q.  */
&& !silent_flag && !question_flag)
message (1, ((file->phony || file->cmds == 0)
? _("Nothing to be done for '%s'.")
: _("'%s' is up to date.")),
file->name);

silent_flag由 make-s命令行选项设置:

https://github.com/mturk/gnumake/blob/9c3fecf4dd76a8c802055d9bd3689d0c6c5d6167/main.c#L412

/* Nonzero means do not print commands to be executed (-s).  */
int silent_flag;
{ 's', flag, &silent_flag, 1, 1, 0, 0, 0, "silent" },

而且它设置在 https://github.com/mturk/gnumake/blob/9c3fecf4dd76a8c802055d9bd3689d0c6c5d6167/file.c#L752

f = lookup_file (".SILENT");
if (f != 0 && f->is_target)
{
if (f->deps == 0)
silent_flag = 1;
else
for (d = f->deps; d != 0; d = d->next)
for (f2 = d->file; f2 != 0; f2 = f2->prev)
f2->command_flags |= COMMANDS_SILENT;
}

这是对 https://www.gnu.org/software/make/manual/html_node/Special-Targets.html 中记录.SILENT特殊目标的解析

.SILENT

如果为 指定先决条件。静音,然后制作不会打印 配方用于在执行这些特定文件之前重新制作它们。 的配方 .静默被忽略。

如果作为没有先决条件的目标提及,.沉默说不要 在执行之前打印任何配方。这种用法".沉默'是 仅支持历史兼容性。我们建议您使用 更有选择性的方式来沉默特定的食谱。请参阅配方回声。 如果您想使特定制作的所有配方静音,请使用 "-s"或"--silent"选项(请参阅选项摘要)。

因此,请尝试将.SILENT特殊目标添加到您的 Makefile,而无需任何其他先决条件。(解决方案未经我测试。

.SILENT: