How to generate Debug symbols with Makefile for C? [Linux]
Asked Answered
A

3

14

I'm trying to use GDB and KDEvelop to debug a console app under Knoppix VM. KDevelop and GDB don't break at my breakpoints. I suspect it's because they don't have debug symbols.

If I'm correct how do I need to change my Makefile to create those. Maybe the problem is somewhere else?

Regards, Ariel

Altheaalthee answered 20/12, 2009 at 23:1 Comment(0)
P
24

Include -g in the flags sent to the compiler and linker. The default variables for this are CFLAGS and LDFLAGS respectively.

The second step: exclude -s from flags (-s means strip)

Paolapaolina answered 20/12, 2009 at 23:3 Comment(2)
The "-g" option only needs to be given to the compiler via the CFLAGS macro. It's not necessary to have it in the LDFLAGS macro.Vile
Cargo cult knowledge, I guess. At some point I used a linker that had to be told not to throw away the debugging symbols...Paolapaolina
N
7

If you are able to see source and set the breakpoint, then you probably have debugging symbols established. However, the usual sequence is:

gcc -g -o (outputname) (source files...)
gdb outputname

Give more specifics about what you are doing and what messages you see and we can be more specific.

Newsworthy answered 20/12, 2009 at 23:4 Comment(0)
M
4

The full example would be:

CFLAGS =-g

all: program.o
    gcc -o program program.o

The CFLAGS here is applied to both the compiler and the linker.

Matey answered 20/12, 2016 at 12:23 Comment(1)
That's not true. In your example -g is only applied in the compile step. This is because compilation happens through an implicit rule of make that creates program.o from program.c (without program.c ever being mentioned explicitly in the makefile. The implicit rule uses CFLAGS. However, the explicit rule for the linking step is executed as shown - thus -g is not applied while linking (AFAIK it does not have to with gcc anyway).Tredecillion

© 2022 - 2024 — McMap. All rights reserved.