C++ gdb for C++ šŸš€

beginner
12 min

C++ gdb for C++ šŸš€

Welcome to our comprehensive guide on using gdb (GNU Debugger) for C++! This tutorial is designed to help both beginners and intermediate learners understand how to effectively use gdb to debug and solve issues in their C++ programs.

šŸŽÆ What is gdb?

gdb is a powerful tool that allows you to debug programs written in C++. It can help you understand what your code is doing, find errors, and fix them.

šŸ’” Why do we need gdb?

Writing a perfect C++ program is a complex task, and it's almost impossible to get it right on the first try. gdb helps you understand the behavior of your program during execution and pinpoint the issues that might be causing it to malfunction.

šŸ“ Setting up gdb

Before we dive into debugging, let's make sure gdb is properly installed and configured on your system.

šŸŽÆ Compiling your C++ program with gdb

To compile and debug a C++ program using gdb, you'll need to use the g++ command with the -g flag. This flag tells g++ to include debugging symbols in the compiled output. Here's an example:

bash
$ g++ -g main.cpp -o main

This command compiles the main.cpp file and generates an executable named main.

šŸ’” Navigating the gdb interface

Once your program is compiled with debugging symbols, you can use gdb to execute and debug it. Start the debugger with the following command:

bash
$ gdb main

gdb's interface might look a bit intimidating at first, but don't worry! We'll guide you through the basics.

šŸŽÆ Basic gdb commands

  • run: Starts the program execution
  • breakpoint: Sets a breakpoint at a specific line
  • continue: Resumes program execution after a breakpoint
  • step: Executes one line of code and stops at the next line
  • print: Displays the value of a variable
  • quit: Exits the debugger

šŸ’” Debugging a simple program

Let's try debugging a simple C++ program with gdb.

cpp
#include <iostream> int main() { int a = 10; int b = 0; std::cout << a / b << std::endl; return 0; }

Save this code as divide.cpp, compile it with gdb, and start debugging:

bash
$ g++ -g divide.cpp -o divide $ gdb divide (gdb) run

The program will crash due to the division by zero error. Now, let's set a breakpoint at the line causing the issue and inspect the variables:

bash
(gdb) break divide.cpp:7 Breakpoint 1 at 0x40060d: file divide.cpp, line 7. (gdb) run

The program will now stop at the breakpoint. You can use the print command to inspect the variables' values:

bash
(gdb) print a $1 = 10 (gdb) print b $2 = 0

Now that you can see the problem, modify the code to avoid division by zero and continue execution:

bash
(gdb) continue Continuing. Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7a50b77 in std::ios_base::Init::Init() () (gdb) list 11 return 0; 12 } 13 14 int main(int argc, char *argv[]) { 15 int a = 10; 16 int b = 0; 17 if (b == 0) { 18 std::cerr << "Error: Division by zero." << std::endl; 19 return 1; 20 } 21 int result = a / b; 22 std::cout << "Result: " << result << std::endl; 23 return 0; 24 } (gdb) print a $1 = 10 (gdb) print b $2 = 0 (gdb) continue Continuing. Result: 10 (gdb) quit A debugging session is active. In order to kill it you can use one of the following commands: (gdb) kill (gdb) quit (gdb) exit

And there you have it! You've just debugged your first C++ program using gdb.

šŸ’” Quiz

Question: What does the g++ -g flag do when compiling a C++ program?

A: Compiles the program B: Compiles the program with debugging symbols C: Runs the compiled program Correct: B Explanation: The g++ -g flag tells g++ to include debugging symbols in the compiled output, making it easier to debug the program using gdb.

Question: What is the purpose of the breakpoint command in gdb?

A: Compiles the program with debugging symbols B: Sets a breakpoint at a specific line in the program C: Resumes program execution after a breakpoint Correct: B Explanation: The breakpoint command sets a breakpoint at a specific line in the program, allowing you to stop the execution and inspect the variables at that point.