How to Use GDB to Debug a C++ Program

Debugging is one of the most important skills for a C++ programmer. Many times your program compiles successfully, but it does not work as expected. This is where GDB (GNU Debugger) helps you.
In this guide, you will learn how to use GDB to debug a C++ program, explained in very simple words, especially for students and beginners.

What is GDB?


GDB stands for GNU Debugger.
It is a powerful tool that helps you:
• Find logical errors
• Stop program execution at specific lines
• Check variable values
• Run code line by line
GDB is mostly used in Linux environments, but it also works on Windows using tools like MinGW or WSL.

Why Use GDB for C++ Debugging?


Using GDB makes debugging easier because:
• You can see where the program stops
• You can inspect variable values
• You can track runtime errors
• You don’t need to add too many cout statements

Step 1: Write a Simple C++ Program


Lets the start with a small C++ program that has a logical issue.

				
					#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int b = 0;
    int result = a / b;

    cout << "Result: " << result << endl;
    return 0;
}

				
			

This program will cause a runtime error (division by zero).

Step 2: Compile C++ Program with Debug Symbols

To use GDB properly, you must compile your program with the -g flag.

g++ -g program.cpp -o program

The -g option tells the compiler to include debugging information.

Step 3: Start GDB

Now run GDB using this command:

gdb program

You will enter the GDB environment.

Step 4: Set a Breakpoint

A breakpoint tells GDB where to stop the program.

break main

Or break at a specific line number:

break 7

Step 5: Run the Program in GDB

Start execution using:

run

The program will stop at the breakpoint before crashing.

Step 6: Step Through the Code

Use these commands:

  • next → move to next line
  • step → go inside functions
  • continue → continue execution

Example:

next

Step 7: Check Variable Values

You can check variable values using:

print aprint bprint result

This helps you understand where the problem is happening.

Step 8: Exit GDB

When done, exit GDB using:

quit

 

Common GDB Commands (Very Important)

Command

Purpose

run

Start program

break

Set breakpoint

next

Execute next line

step

Go inside function

print

Show variable value

continue

Resume execution

quit

Exit GDB

Real-World Use of GDB

  • Debugging large C++ projects
  • Fixing segmentation faults
  • Understanding complex logic
  • Exam and lab debugging

Common Mistakes Beginners Make

  • Forgetting -g flag while compiling
  • Not setting breakpoints
  • Using cout instead of debugger
  • Ignoring runtime errors

Recommended : Binary to Gray Code Conversion C Program in C++

Is GDB difficult for beginners?

No, GDB looks complex at first but becomes easy with practice.

Do I need GDB for small programs?

Yes, learning GDB early saves a lot of time later.

Can GDB debug runtime errors?

Yes, GDB is best for runtime and logical errors.

Does GDB work on Windows?

Yes, using MinGW, Cygwin, or WSL.

Leave a Reply

Your email address will not be published. Required fields are marked *