You are not logged in.

#1 2010-10-26 02:13:32

Basu
Member
From: Cornell University
Registered: 2006-12-15
Posts: 296
Website

[Solved] Using GDB on multiple files

I'm building a smallish C project spread across multiple files. I'm trying to use GDB to debug, but it doesn't let me place breakpoints in source files outside the one containing main(). I can set breaks using the function name, but I want to set it at specific lines in other files. This is the Makefile I'm using:

test: test.c bootstrap.o object.o
    gcc $(CFLAGS) -g -Wall -o test test.c bootstrap.o object.o -lpthread

bootstrap: object.h bootstrap.h bootstrap.c
    gcc $(CFLAGS) -c -g -o bootstrap.o bootstrap.c

object: object.h object.c
    gcc $(CFLAGS) -c -g -o object.o object.c -lpthread

clean: object.o
    rm *.o

From what I've read online, using the -g flag should allow GDB to access all the source files, but it is not. I don't know if I'm supposed to pass a separate linker argument. Any help is appreciated.

PS. Are there any standard C debuggers out there besides GDB?

Last edited by Basu (2010-10-27 23:00:37)


The Bytebaker -- Computer science is not a science and it's not about computers
Check out my open source software at Github

Offline

#2 2010-10-26 02:54:19

diegonc
Member
Registered: 2008-12-13
Posts: 42

Re: [Solved] Using GDB on multiple files

You have to state the filename. Like this:

(gdb) list object.c:1
...
5  void some_interesting_function()
6  {
7       int a = 0;
...
n }
...
(gdb) break object.c:7

Offline

#3 2010-10-26 04:30:21

Basu
Member
From: Cornell University
Registered: 2006-12-15
Posts: 296
Website

Re: [Solved] Using GDB on multiple files

That's what I'm doing, but it tells me that there is no source file named object.c


The Bytebaker -- Computer science is not a science and it's not about computers
Check out my open source software at Github

Offline

#4 2010-10-26 07:46:02

ber_t
Member
From: Berlin, Germany
Registered: 2010-03-10
Posts: 214
Website

Re: [Solved] Using GDB on multiple files

Well, gdb just does not find the source files the program was compiled from.
Are you starting gdb inside the build directory? Otherwise please have a look at: http://web.mit.edu/gnu/doc/html/gdb_9.html#SEC51

Offline

#5 2010-10-26 17:39:44

Basu
Member
From: Cornell University
Registered: 2006-12-15
Posts: 296
Website

Re: [Solved] Using GDB on multiple files

I am starting GDB from inside the build directory. Doing a "show directories" shows $cdir:$cwd, but printing either of them gives void.


The Bytebaker -- Computer science is not a science and it's not about computers
Check out my open source software at Github

Offline

#6 2010-10-26 18:50:31

ber_t
Member
From: Berlin, Germany
Registered: 2010-03-10
Posts: 214
Website

Re: [Solved] Using GDB on multiple files

The fault is in your Makefile, sorry that I did not see it before creating my first post:

bootstrap: object.h bootstrap.h bootstrap.c

should be:

bootstrap.o: object.h bootstrap.h bootstrap.c

Because there are no rules for test's dependencies bootstrap.o and object.o, they're created by a default command, which does not use the -g flag.

Your Makefile is too cumbersome. Instead of creating a single rule for every target/dependency, you should generalize them wherever it's possible:

CC?=gcc
CFLAGS+= -g -Wall
LDFLAGS=
LIBS=-lpthread

test: bootstrap.o object.o test.o
    $(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

%.o: %.c
    $(CC) $(CFLAGS) -c -o $@ $^

clean:
    rm *.o test

Why is there an object.o dependency for the target clean? Calling `make clean' without object.o would first create it and then delete it directly afterwards.

Offline

#7 2010-10-27 03:27:13

Basu
Member
From: Cornell University
Registered: 2006-12-15
Posts: 296
Website

Re: [Solved] Using GDB on multiple files

Thanks a lot, that fixed it. This is the first time I'm writing Makefiles by hand. Previously I used Qmake to autogenerate Makefiles. I'm just getting started so I'm learning as I go.


The Bytebaker -- Computer science is not a science and it's not about computers
Check out my open source software at Github

Offline

Board footer

Powered by FluxBB