I'm using a makefile to compile a program made of many .c
files, and any time make
is invoked it only compiles those files modified after the last run (nothing special until here).
To avoid cluttering my screen, I prepend @
at the beginning of each $(CC)
call, and before it I print a customized echo
message. For example:
%.o: %.c $(h1) $(h3) %.h
@echo -e "\tCompiling <" $<
@$(CC) $(CFLAGS) -c $< -o $(libDir)$@$(MATHOPTS)
My question is: how can I control the verbosity of make
in a more "dynamic way", in order to be able to:
- Normal behaviour: only a customized message is printed for every makefile rule executed.
- Verbose behaviour: print the command actually executed by every makefile rule (as if the
@
wasn't used at all).
Instead of using "@gcc" to compile, you can omit that "@" and pass the "-s" option to your make command instead. (Leave "@echo" as it is.) Then "make -s" would be your brief make command, and "make" would be verbose.
From the GNU Make manual pages
(The other answers better answer your question, but this approach deserves a mention.)
Another solution (one which I like because it's flexible)
You can skip the vecho stuff, but it does come in handy at times.
Since I can't comment on the
AT = $(AT_$(V))
suggestion, note that Automake does provide a standard macro that does the same thing asAT
, which is calledAM_V_at
.You will also find that it has another very useful
AM_V_GEN
variable, that resolves either to nothing or to@echo " GEN " $@;
, depending on the verbosity.This allows you to code something like this:
The output of which will be either (verbosity disabled):
or (verbosity enabled):
Pretty convenient, and this removes the need to define your own macros.
I would create a function which takes a command to execute and decides whether to echo it.
Then the result of plain
make
invocation will be something like:Whereas
make V=1
will give:I'd do it the way automake does:
If you need to execute other commands in your rules, I like the following snippet. Write
$(AT)
instead of@
and it will be silent whenV=0
but printed whenV=1
.