gcc -g Option

The -g option in gcc generates debugging information that enables source-level debugging with tools like gdb. This information includes line numbers, variable names, function names, and type information.

Syntax

gcc -g sourcefile.c -o program

-g Option Details

  • Purpose: Generate debugging information for debuggers
  • Format: Uses DWARF debugging format by default
  • Performance: No runtime performance impact
  • File Size: Increases executable size due to debug symbols
  • Compatibility: Works with gdb, lldb, and other debuggers

Description

The -g option instructs gcc to include debugging information in the compiled program. This information allows debuggers to map machine code back to source code, enabling line-by-line debugging.

Debug information includes:

  • Source file names and line numbers
  • Variable names and types
  • Function names and parameters
  • Structure and class definitions
  • Call stack information

Debug Levels

Option Level Description
-g0 None No debugging information (default)
-g1 Minimal Line numbers and external symbols only
-g or -g2 Default Full debugging information
-g3 Extra Includes macro definitions

Essential GDB Commands

Starting & Running:
  • gdb ./program - Start debugger
  • run - Run the program
  • quit - Exit gdb
Breakpoints:
  • break main - Break at function
  • break file.c:10 - Break at line
  • info breakpoints - List breakpoints

Detailed Examples

Basic debugging compilation

gcc -g -o debug_program program.c
gdb ./debug_program

Compiles with debug info and starts debugging session

Complete debugging session

# Compile with debug info
gcc -g -o calculator calculator.c

# Start debugging
gdb ./calculator
(gdb) break main
(gdb) run
(gdb) step
(gdb) print variable_name
(gdb) continue
(gdb) quit

Full debugging workflow with breakpoints and variable inspection

Debugging with core dumps

gcc -g -o program program.c
./program # Program crashes and creates core dump
gdb ./program core
(gdb) backtrace
(gdb) info locals

Analyze program crashes using core dumps

Combining with optimization

gcc -g -O2 -o optimized_debug program.c
gdb ./optimized_debug

Debug optimized code (some debug info may be limited)

Multiple files with debug info

gcc -g -c main.c utils.c math.c
gcc -g main.o utils.o math.o -o program
gdb ./program
(gdb) break utils.c:25

Debug multi-file projects with line-specific breakpoints

Debugging Tips

  • Always use -g: Include debug info even in development builds
  • Disable optimization: Use -O0 for easier debugging
  • Core dumps: Enable with 'ulimit -c unlimited'
  • Valgrind: Combine with memory debugging tools
  • Strip symbols: Use 'strip' command for release builds

📊 Performance Considerations

  • Runtime: No performance impact during execution
  • File Size: Significantly increases executable size
  • Compilation: Slightly slower compilation time
  • Memory: Debugger uses more memory when loading symbols
  • Distribution: Strip debug symbols for release versions

See also