112

I keep getting this error mesage when trying to add a breakpoint in gdb.

I've used these commands to compile:

gcc -g main.c utmpib2.c -o main.o
and:
cc -g main.c utmpib2.c -o main.o
and also:
g++ -g main.c utmpib2.c -o main.o

I also tried "-ggdb" instead of "-g" and I still get that error message.

I then execute gdb:

$gdb

In gdb:

(gdb)exec-file main.o
(gdb)break 59
No symbol table is loaded. Use the "file" command.
user994165
  • 9,146
  • 30
  • 98
  • 165
  • Oh I meant main.o. I updated it. I tried also using "-ggdb" and it is still giving me the same problem. – user994165 Feb 12 '12 at 01:51
  • Show us exactly how are you invoking gcc and gdb. Copy-paste to avoid any erors. – Piotr Praszmo Feb 12 '12 at 01:53
  • 1
    I updated my commands. This is really weird. It just started working. I know previously I was accessing gdb using "gdb a.out" and I was getting an error message about a.out not existing or something. Then I switched to the "exec-file". Now I tried with a.out and it says "This GDB was configured as "i486-linux-gnu" " and breakpoints can be set. – user994165 Feb 12 '12 at 02:08
  • oh duh I was specifying the wrong file a.out. I followed along a gdb tutorial without thinking to change the filename to my own. – user994165 Feb 12 '12 at 02:12

5 Answers5

163

You have to add extra parameter -g, which generates source level debug information. It will look like:

gcc -g prog.c

After that you can use gdb in common way.

wono
  • 743
  • 4
  • 6
alxndr
  • 2,349
  • 3
  • 17
  • 14
75

First of all, what you have is a fully compiled program, not an object file, so drop the .o extension. Now, pay attention to what the error message says, it tells you exactly how to fix your problem: "No symbol table is loaded. Use the "file" command."

(gdb) exec-file test
(gdb) b 2
No symbol table is loaded.  Use the "file" command.
(gdb) file test
Reading symbols from /home/user/test/test...done.
(gdb) b 2
Breakpoint 1 at 0x80483ea: file test.c, line 2.
(gdb) 

Or just pass the program on the command line.

$ gdb test
GNU gdb (GDB) 7.4
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
[...]
Reading symbols from /home/user/test/test...done.
(gdb) b 2
Breakpoint 1 at 0x80483ea: file test.c, line 2.
(gdb) 
Kevin
  • 53,822
  • 15
  • 101
  • 132
10

I have the same problem and I followed this Post, it solved my problem.

Follow the following 2 steps:

  1. Make sure the optimization level is -O0
  2. Add -ggdb flag when compiling your program

Good luck!

Charles Chow
  • 575
  • 1
  • 8
  • 12
2

Whenever gcc on the compilation machine and gdb on the testing machine have differing versions, you may be facing debuginfo format incompatibility.

To fix that, try downgrading the debuginfo format:

gcc -gdwarf-3 ...
gcc -gdwarf-2 ...
gcc -gstabs ...
gcc -gstabs+ ...
gcc -gcoff ...
gcc -gxcoff ...
gcc -gxcoff+ ...

Or match gdb to the gcc you're using.

ulidtko
  • 14,740
  • 10
  • 56
  • 88
1

I met this issue this morning because I used the same executable in DIFFERENT OSes: after compiling my program with gcc -ggdb -Wall test.c -o test in my Mac(10.15.2), I ran gdb with the executable in Ubuntu(16.04) in my VirtualBox.

Fix: recompile with the same command under Ubuntu, then you should be good.

fishstick
  • 2,144
  • 1
  • 19
  • 14