Search code examples
cgccmakefileassert

FLAG=-DNDEBUG is not disabling assert() in C


I have a bunch of assert() functions I used throughout my C files and from reading I have done I should be able to disable the assertions by passing in a command line parameter like so:

make

Doing this does not disable the assertions. However, adding into the code, #define NDEBUG does disable the assertions. I want to disable them from the command line though. Is there a reason why this flag is not working correctly?

I am on a Windows machine.

Here is the makefile:

OPTIONS = -B CFLAGS=-DNDEBUG -ansi -pedantic -Wall -Werror

a.out: myProgram.o StudentImplementation.o ListImplementation.o
    gcc $(OPTIONS) myProgram.o StudentImplementation.o ListImplementation.o

myProgram.o: myProgram.c StudentInterface.h StudentType.h ListInterface.h ListType.h
    gcc $(OPTIONS) -c myProgram.c

StudentImplementation.o: StudentImplementation.c StudentInterface.h StudentType.h
    gcc $(OPTIONS) -c StudentImplementation.c

ListImplementation.o: ListImplementation.c ListInterface.h ListType.h StudentInterface.h StudentType.h
    gcc $(OPTIONS) -c ListImplementation.c

clean:
    rm *.o a.out

Solution

  • If you have a normal makefile or no makefile, then the command you want is

    make -B CFLAGS=-DNDEBUG
    

    There is no FLAG variable in the standard make recipes; each component has its own variable, so CFLAGS is for C, CXXFLAGS is for C++, LDFLAGS is for the linker, and so on.

    With the Makefile you provide in the question, you cannot change flags on the make command line. You could use

    OPTIONS = -DNDEBUG -ansi -pedantic -Wall -Werror
    

    but that means editing your Makefile every time you want to change the debug setting.