In order to use the debugger with your source code, you must first compile it
with the -g flag.
linux3 % gcc -g -Wall -ansi example1.c
linux3 % a.out
The -g flag tells the compiler to leave information about your code
in the object (.o) and executable files. The information allows gdb and other
debuggers to show your code the way you're used to seeing it. If the code
compiles cleanly, you can then run gdb, using the name of your
executable as an argument:
linux3 % gdb a.out
GNU gdb 19991004
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
The (gdb) prompt tells you that you are in the debugger. First,
let's list the source code. With no arguments, the list
command will list 10 lines of code, beginning at the prompt. You can specify
ranges with a comma.
(gdb) list 1,26
4 int main()
6 int i;
8 for (i = 0; i < 5; ++i)
10 switch (i % 5)
12 case 2:
13 case 3:
14 printf("* *\n");
16 case 1:
17 case 4:
18 printf(" *******\n ");
21 printf(" Beginning:\n ");
25 return 0;
As you can see, the lines are numbered.