Search code examples
c++makefilecompilationgnu-makecompiler-flags

Append compiler flags when running make


I would like to append flags to the compiler flags when running make, without altering the Makefile in anyway, e.g.

make CXX_FLAGS+='-DDEBUG'

The above treats "+=" as "=", so it's not the correct symbol.


Solution

  • You just have to modify the variable as override in your Makefile once. And then you can do what you want to do.

    Here's the example,

    Makefile:

    override CFLAGS+=-g                                                                                    
    
    app: main.c
      gcc $(CFLAGS) -o app main.c
    

    Run the make:

    $ make
    gcc -g -o app main.c
    

    Append the '-Wall' to $CFLAGS from the command:

    $ make CFLAGS=-Wall
    gcc -Wall -g -o app main.c
    

    Work fine here. And here's manual you can reference.