-->
Previous Table of Contents Next


Optimization Options

When you compile C code with GCC, it tries to compile the code in the least amount of time and also tries to create compiled code that is easy to debug. Making the code easy to debug means that the sequence of the compiled code is the same as the sequence of the source code, and no code gets optimized out of the compile. There are many options that you can use to tell GCC to create smaller, faster executable programs at the cost of compile time and ease of debugging. Of these options, the two that you typically use are the -O and the -O2 options.

The -O option tells GCC to perform basic optimizations on the source code. In most cases these optimizations make the code run faster. The -O2 option tells GCC to make the code as fast and small as it can. The -O2 option causes the compilation speed to be slower than when using the-O option, but typically results in code that executes more quickly.

In addition to the -O and -O2 optimization options, there are a number of lower-level options that can be used to make the code faster. These options are very specific and should be used only if you fully understand the consequences that these options will have on the compiled code. For a detailed description of these options, refer to the GCC man page by typing man gcc on the command line.

Debugging and Profiling Options

GCC supports several debugging and profiling options. Of these options, the two that you are most likely to use are the -g option and the -pg option.

The -g option tells GCC to produce debugging information that the GNU debugger (gdb) can use to help you to debug your program. GCC provides a feature that many other C compilers do not have. With GCC you can use the -g option in conjunction with the -O option (which generates optimized code). This can be very useful if you are trying to debug code that is as close as possible to what will exist in the final product. When you are using these two options together, you should be aware that some of the code that you have written will probably be changed by GCC when it optimizes it. For more information on debugging your C programs, see the “Debugging GCC Applications with gdb” section in this chapter.

The -pg option tells GCC to add extra code to your program that will, when executed, generate profile information that can be used by the gprof program to display timing information about your program. For more information on gprof, see the “gprof” section in this chapter.

Debugging GCC Programs with gdb

Linux includes the GNU debugging program called gdb. gdb is a very powerful debugger that can be used to debug C and C++ programs. It enables you to see the internal structure or the memory that is being used by a program while it is executing. Some of the functions that gdb provides for you are these:

  It enables you to monitor the value of variables that are contained in your program.
  It enables you to set breakpoints that will stop the program at a specific line of code.
  It enables you to step through the code, line by line.

You can run gdb by typing gdb on the command line and pressing Enter. If your system is configured properly, gdb will start and you will see a screen that resembles the fol-lowing:


GDB is free software and you are welcome to 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.

GDB 4.12 (i486-unknown-linux), Copyright 1994 Free Software Foundation,

Inc.

(gdb)

When you start gdb, there are a number of options that you can specify on the command line. You will probably run gdb in the following way:


gdb <fname>

When you invoke gdb in this way, you are specifying the executable file that you want to debug. This tells gdb to load the executable file with the name fname. There are also ways of starting gdb that tell it to inspect a core file that was created by the executable file being examined, or to attach gdb to a currently running process. To get a listing and brief description of each of these other options, you can refer to the gdb man page or type gdb -h at the command line.

Compiling Code for Debugging

To get gdb to work properly, you must compile your programs so that debugging information is generated by the compiler. The debugging information that is generated contains the types for each of the variables in your program as well as the mapping between the addresses in the executable program and the line numbers in the source code. gdb uses this information to relate the executable code to the source code.

To compile a program with the debugging information turned on, use the -g compiler option.


Previous Table of Contents Next