Suppress messages in make clean (Makefile silent remove)

后端 未结 5 2002
感情败类
感情败类 2021-01-30 13:10

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

clean:
    rm -fr *.o

this rule will print:

$>make clean   
rm -fr          


        
5条回答
  •  一向
    一向 (楼主)
    2021-01-30 13:23

    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.

提交回复
热议问题