Valgrind and Memory Leaks

How to Compile a C Program for GDB

Compile a C program with -g and -O0 so GDB shows line numbers and variables, then start a session with gdb ./program and step through your code.

On this page
  1. The short answer
  2. Why GDB needs debug information
  3. Compile a C program
  4. The same steps for C++
  5. Start a debugging session
  6. Useful commands
  7. Check the build
  8. macOS note
  9. Using the same build with Valgrind

The short answer

Compile with -g to include debugging information, and use -O0 to turn off optimization. Then start GDB with gdb ./program. The steps are the same for C and C++. The C example below uses gcc, and a C++ file uses g++.

Why GDB needs debug information

The compiler writes debug information when you pass -g. GDB uses it to match machine code to your source, so it can show variable names, line numbers, and the line it is running. Without it, GDB still runs the program, but it reports that there are no debugging symbols.

-O0 turns off optimization. Optimizing compilers can remove variables, merge lines, or reorder code, so GDB may show <optimized out> for a value you expect to see.

Compile a C program

Here is a short C program to debug:

/* hello.c */
#include <stdio.h>

int main(void) {
    int total = 0;
    for (int i = 1; i <= 3; i++) {
        total += i;
    }
    printf("total = %d\n", total);
    return 0;
}

Compile it with debug information and warnings turned on:

gcc -g -O0 -Wall -Wextra -o hello hello.c

The same steps for C++

For a C++ file such as hello.cpp, the command is the same with g++:

g++ -g -O0 -Wall -Wextra -o hello hello.cpp

Start a debugging session

Start GDB with the program as the argument:

gdb ./hello

At the (gdb) prompt, try these commands:

(gdb) break main
(gdb) run
(gdb) next
(gdb) print total
(gdb) continue

break main pauses the program at the start of main. run starts it, and it stops at that breakpoint. next runs the current line and stops at the next one. print total shows the current value of total, and continue runs the program until it exits or hits another breakpoint.

Useful commands

Command Short form What it does
break main b main Pause at the start of main
run r Start the program
next n Run one line, stepping over function calls
step s Run one line, entering function calls
print total p total Show the value of a variable
info locals List the local variables in the current function
backtrace bt Show the chain of function calls
continue c Run until the next breakpoint or exit
quit q Leave GDB

Check the build

If GDB reports no debugging symbols, the program was built without -g. Recompile it with -g, then start GDB again.

macOS note

On macOS, GDB needs extra setup before it can debug programs. Many Mac developers use lldb instead, which reads the same -g debug information.

Using the same build with Valgrind

Valgrind also shows line numbers only with -g, so one debug build works for both tools. For the Valgrind steps, see how to use valgrind in c.

GDB helps most when a program crashes, often with a segmentation fault. For what causes that crash, see what is segmentation fault in c.

The complete guide to valgrind and memory leaks

How to Use Valgrind With C++
Read the guide

Keep exploring

Guides from other topics

All topics →