Compiling a Program63
3.3.4Compilation with Debug Support
If you want to debug a program after compiling with
gcc
, you have to include debug
information in the compiled program. The debug information is included in object file using the
–
g
command line switch with
gcc
. The following command creates the
hello.o
file that con-
tains debug information.
[rr@conformix 4]$ gcc -g -c hello.c
[rr@conformix 4]$
Note that when you compile a program with debug information, the size may be quite
large as compared to a file compiled without debug information. In the example program of
hello.c
, the size of
hello.o
is 908 bytes when compiled without debug information. The
size of
hello.o
is 10780 bytes when it is compiled with debug information.
You can use multiple debug levels with –
g
option. The default debug level is 2 which is
equivalent to using –
g2
command line option. If you use –
g3
command line option, information
about macros is also included which makes it easier to debug macros.
With the –
a
option on the command line, you can also include some profiling information
in the object code.
You can also use some command line switches to provide extra information. For example,
one useful thing is to print out a list of directories that the
gcc
command searches to find files.
The following command will print all directories that
gcc
uses to search libraries, programs and
so on.
[rr@conformix 4]$ gcc -print-search-dirs hello.c -o hello
install: /usr/lib/gcc-lib/i386-redhat-linux/2.96/
programs: =/usr/lib/gcc-lib/i386-redhat-linux/2.96/:/usr/lib/
gcc-lib/i386-redhat-linux/2.96/:/usr/lib/gcc-lib/i386-redhat-
linux/:/usr/lib/gcc/i386-redhat-linux/2.96/:/usr/lib/gcc/i386-
redhat-linux/:/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../
../../i386-redhat-linux/bin/i386-redhat-linux/2.96/:/usr/lib/
gcc-lib/i386-redhat-linux/2.96/../../../../i386-redhat-linux/
bin/
libraries: =/usr/lib/gcc-lib/i386-redhat-linux/2.96/:/usr/lib/
gcc/i386-redhat-linux/2.96/:/usr/lib/gcc-lib/i386-redhat-
linux/2.96/../../../../i386-redhat-linux/lib/i386-redhat-
linux/2.96/:/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../
../i386-redhat-linux/lib/:/usr/lib/gcc-lib/i386-redhat-linux/
2.96/../../../i386-redhat-linux/2.96/:/usr/lib/gcc-lib/i386-
redhat-linux/2.96/../../../:/lib/i386-redhat-linux/2.96/:/lib/
:/usr/lib/i386-redhat-linux/2.96/:/usr/lib/
[rr@conformix 4]$
N O T E You can use the debug option with optimization
options. Optimization options are discussed later in this chapter.