Search code examples
gnu-makegnugcc-warning

Suppress warnings when building with GNU make in command line


Is there a way to suppress compiler warnings for GNU make and only show higher-order logs, i.e. errors?

Apparently, this should be possible using make -w as described here. However, for my version of GNU make (4.1), the man file specifies this as printing the current directory:

-w, --print-directory     Print the current directory.
-W FILE                      Consider FILE to be infinitely new.

If possible, this should be disabled both for make-internal warnings ($(warning ...)) and compiler-level warnings by gcc.


Solution

  • As pointed out in this post, it is not possible to directly add flags for the compiler. Furthermore, adding to existing CFLAGS variables (make CFLAGS+=-w) does not work either in most cases, as it ignores the append part and simply redefines the variable in the command line.

    A very easy solution to fix this is by creating an empty dummy variable (once) inside your makefile and then defining it in case you need it:

    # Add empty variable to add flags over command line
    CDBG +=
    CFLAGS += $(CDBG)
    

    Which you then simply use as follows:

    make CDBG=-w