Damian H
Damian H

Reputation: 77

Using gdb with make

I want to use gdb with my application for debug throught all the libraries. I use make for compile the application, and if I try gdb it says no debugging symbols found.

How I can modify my makefile for use -g flag?

hospital: hospital.o config.o utils.o message.o semaphore.o sharedmem.o logger.o
    gcc -o hospital hospital.o config.o utils.o message.o semaphore.o sharedmem.o logger.o
hospital.o: hospital.c
    gcc -c hospital.c
sharedmem.o: sharedmem.c sharedmem.h
    gcc -c sharedmem.c
message.o: message.c message.h
    gcc -c message.c
semaphore.o: semaphore.c semaphore.h
    gcc -c semaphore.c
config.o: config.c config.h
    gcc -c config.c
logger.o: logger.c logger.h
    gcc -c logger.c
utils.o: utils.c utils.h
    gcc -c utils.c
clean:
    rm -f *.o

Upvotes: 1

Views: 9863

Answers (2)

Erik Nyquist
Erik Nyquist

Reputation: 1317

The answer to what you're asking, is to add the '-g' switch when you compile the object files, i.e.

hospital: hospital.o config.o utils.o message.o semaphore.o sharedmem.o     logger.o
    gcc -o hospital hospital.o config.o utils.o message.o semaphore.o     sharedmem.o logger.o
hospital.o: hospital.c
    gcc -g -c hospital.c
sharedmem.o: sharedmem.c sharedmem.h
    gcc -g -c sharedmem.c
message.o: message.c message.h
    gcc -g -c message.c
semaphore.o: semaphore.c semaphore.h
    gcc -g -c semaphore.c
config.o: config.c config.h
    gcc -g -c config.c
logger.o: logger.c logger.h
    gcc -g -c logger.c
utils.o: utils.c utils.h
    gcc -g -c utils.c
clean:
    rm -f *.o

But, a Makefile like this is really hard to maintain. Make can do a lot of this stuff for you, check out this example which should work well for your project. Simply add '-g' to the CFLAGS variable.

Even something smaller, like this, would do what you want:

PROGRAM := hostpital
SRCS := $(wildcard *.c)
OBJS := ${SRCS:.c=.o}
CFLAGS=-g

$(PROGRAM): $(OBJS)
    $(CC) $(OBJS) -o $(PROGRAM)

clean:
    @- $(RM) $(PROGRAM)
    @- $(RM) $(OBJS)

NOTE: Makefile needs tabs preceding the actions. Replace leading spaces with a tab.

Upvotes: 5

Barmar
Barmar

Reputation: 782427

Add the -g option to all the compilations.

hospital: hospital.o config.o utils.o message.o semaphore.o sharedmem.o logger.o
    gcc -o hospital hospital.o config.o utils.o message.o semaphore.o sharedmem.o logger.o
hospital.o: hospital.c
    gcc -g -c hospital.c
sharedmem.o: sharedmem.c sharedmem.h
    gcc -g -c sharedmem.c
message.o: message.c message.h
    gcc -g -c message.c
semaphore.o: semaphore.c semaphore.h
    gcc -g -c semaphore.c
config.o: config.c config.h
    gcc -g -c config.c
logger.o: logger.c logger.h
    gcc -g -c logger.c
utils.o: utils.c utils.h
    gcc -g -c utils.c
clean:
    rm -f *.o

But you would probably be better off using the default rules for compiling, instead of entering all the .c -> .o actions explicitly. The default rules use $(CPPFLAGS) $(CFLAGS), which you can set on the command line. So when you want to compile with debugging enabled, you do:

make clean
make CFLAGS=-g hospital

Upvotes: 3

Related Questions