Include debugging feature in makefile

Hi,

I would like to add a debugging feature in a makefile (flag -ggdb of g++). A quick and dirty solution worked only partially. Debugging was possible but the gnu debugger reported that no debugging symbols found. That was due to the linking of the files which was done separately from the final compilation. The makefile first generates single object files from all source code files and I do not know how to include the flag -ggdb flag in this compilation. Can somebody tell me what I need to do to include the -ggdb option in the first compilation of the source files? Ideally the proper way to do that? Here is the makefile content:

CC=g++ # define the compiler to use
TARGET=greens# define the name of the executable
SOURCES=analyzenet.cpp bicgstab.cpp blood.cpp cmgui.cpp contour.cpp contr_lines.cpp contr_shade.cpp convect.cpp eval.cpp gaussj.cpp greens.cpp histogram.cpp initgreens.cpp input.cpp ludcmp.cpp main.cpp nrutil.cpp outboun.cpp picturenetwork.cpp postgreens.cpp putrank.cpp setuparrays0.cpp setuparrays1.cpp setuparrays2.cpp testconvect.cpp tissrate.cpp # list source files

CFLAGS=-O3
LFLAGS=-Wall -lm -std=c++17

# define list of objects
OBJSC=$(SOURCES:.c=.o)
OBJS=$(OBJSC:.cpp=.o)

# the target is obtained linking all .o files
# for mac: remove -lstdc++fs tag following $(TARGET)
all: $(SOURCES) $(TARGET)

$(TARGET): $(OBJS)
$(CC) $(LFLAGS) $(OBJS) -o $(TARGET) -lstdc++fs

purge: clean
rm -f $(TARGET)

clean:
rm -f *.o
Last edited on
Maybe this. I've reorganized your flags a little. You probably don't want to debug with -O3, so I replace that with -ggdb.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
TARGET = greens
SOURCES := $(wildcard *.cpp)   # assuming you want all .cpp files in current directory
OBJS = $(SOURCES:.cpp=.o)

CC = g++
CPPFLAGS = -std=c++17 -Wall -ggdb
LDFLAGS = -lm -lstdc++fs

all: $(TARGET)

$(TARGET): $(OBJS)
	$(CC) $(CPPFLAGS) $^ -o $@ $(LDFLAGS)

purge: clean
	rm -f $(TARGET)

clean:
	rm -f *.o


CPPFLAGS will automatically be used by implicit rules when compiling a C++ program, so that's how you can pass the -ggdb flag.
Last edited on
Works nicely! Thanks!!
Topic archived. No new replies allowed.