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.
Table of Contents
ToggleWhat 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
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 |
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.
What’s Taking place i’m new to this, I stumbled upon this I’ve found
It absolutely helpful and it has aided me out loads.
I’m hoping to contribute & aid different customers like its helped me.
Great job.
Thanks for your marvelous posting! I genuinely enjoyed reading it,
you are a great author. I will make sure to bookmark your
blog and may come back in the future. I want to encourage that you continue your great posts,
have a nice holiday weekend!
Useful content for readers interested in ai next
gen ethics ai sustainable ai and quantum ai as part of next generation innovation
Hi friends, its enormous paragraph concerning educationand fully explained, keep
it up all the time.
Usually I don’t read article on blogs, but I would like
to say that this write-up very compelled me to try and do it!
Your writing style has been surprised me. Thank you, very nice article.
This article about property law in Thailand is incredibly informative!
As someone who’s been involved in property transactions in Phuket, I find the explanation about land title deeds very practical.
Having a property legal expert is essential when dealing with contracts, especially in a high-demand market like Phuket.
Thanks for sharing such clear and useful insights
on Thai land law.
I read this piece of writing fully about the difference of newest
and previous technologies, it’s awesome article.
hello!,I love your writing so much! percentage we be in contact extra
about your article on AOL? I need an expert in this house to
solve my problem. Maybe that is you! Having a look ahead to see you.