Reputation: 31266
I have a program that outputs things--numbers--to stdout.
The program runs with make run input.txt 0.87 > output_file
in an auto-grader (this is for an assignment). The program must run this way (even though it is not really supposed to be possible for a make file).
However, I got it working with the help of a SO question on here. Here is my makefile:
pr.exe : main.cpp
g++ -std=c++11 $^ -o $@
run:
@./pr.exe $(filter-out $@,$(MAKECMDGOALS))
%:
@:
compile : pr.exe
.PHONY : compile
There is just one problem: the command make run input.txt 0.87
outputs, in addition to the program output, the final line:
make: 'input.txt' is up to date.
How do I suppress this line in my make file such that only the program output is redirected into the file?
It must run properly with only the following command (no make arguments, nada):
make run input.txt 0.87 > some_output_file.txt
Upvotes: 1
Views: 316
Reputation: 94445
The printing of message
is disabled by -s
option of make:
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);
The silent_flag
is set by -s
command line option of make:
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" },
And also it is set in 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;
}
This is parsing of .SILENT
special target documented in https://www.gnu.org/software/make/manual/html_node/Special-Targets.html
.SILENT
If you specify prerequisites for .SILENT, then make will not print the recipe used to remake those particular files before executing them. The recipe for .SILENT is ignored.
If mentioned as a target with no prerequisites, .SILENT says not to print any recipes before executing them. This usage of ‘.SILENT’ is supported only for historical compatibility. We recommend you use the more selective ways to silence specific recipes. See Recipe Echoing. If you want to silence all recipes for a particular run of make, use the ‘-s’ or ‘--silent’ option (see Options Summary).
So, try to add .SILENT
special target to your Makefile without any additional prerequisites. (Solution is not tested by me.)
.SILENT:
Upvotes: 2