0
votes

In my Makefile I am using the -DDEBUG Flag for debugging which works fine (compiles and right output) in the following minimal example:

# Makefile
all : debug

CXX = g++
CXXFLAGS = -std=c++11 -Werror -Wall -Wextra -Wno-unused-value
SOURCES = main.cpp vector.cpp
OBJECTS = $(subst .cpp,.o, $(SOURCES))

debug: $(OBJECTS)
    $(CXX) $(CXXFLAGS) -DDEBUG -o Program $(OBJECTS)
    ./Program

clean:
    rm *.o Program

.PHONY: clean debug

But when i copy all project-files into the Folder the Debug-Flag wont be set (tested with #ifdef DEBUG and cout). I am adding the following files:

ind.hpp ind.cpp debug.hpp debug.cpp

I first thought debug.* could be the problem, but ".PHONY: clean debug" didn't help.

1
You aren't building the object files with the DEBUG in that makefile. That isn't working the way you expect. You need that on the compilation line not the linking line. Add it to CXXFLAGS.Etan Reisner
BTW, the opposite convention of setting NDEBUG for non-debug forms (and not setting NDEBUG when debugging!) is followed by assert(3)Basile Starynkevitch
Unless you really want to learn how to write a Makefile, I would suggest you use a tool to generate it. I like cmake. The old way is the automake (which is pretty terrible, if you ask me). There are others too. These would create the correct Makefile for you.Alexis Wilke

1 Answers

2
votes

In the Makefile change this:

debug: $(OBJECTS)
    $(CXX) $(CXXFLAGS) -DDEBUG -o Program $(OBJECTS)

to:

debug: $(SOURCES)
    $(CXX) $(CXXFLAGS) -DDEBUG -o Program $(SOURCES)

The reason being that in the rule to build debug target you are just linking the object files and not compiling with -DDEBUG. With this change you should be able to compile with -DDEBUG flag.