C++ Debugging - How To Use GDB [Code + Command Line Walkthrough] (2024)

Introduction - Debugging?

Debugging is necessary for any program of any size. It's inevitable that our code will return incorrect results, and we'll want to know what values are along the way to see where things are going wrong. For Linux, "gdb" (GNU Debugger) is one tool used for debugging C and C++ code. It can be used for larger programs, but we're going to keep things simple in this tutorial by showing how to use gdp with basic commands, for a simple program.

Installation

We'll assume that you're running Ubuntu 16.04 for this gdb installation, but the requirements are very basic. (We'll also assume that you have GCC installed to compile our C++ programs.

Run the following command in your terminal to install gdb and necessary dependencies:

sudo apt-get install libc6-dbg gdb valgrind 

And that's it.

A Basic C++ Program (With Some Bugs)

We're going to create a basic C++ program, and it's going to have a few bugs. We'll need to go through the code (with a debugger) to see why we aren't getting the expected result.

First of all, in your project directory, use the the following commands to create a makefile and our cpp file.

touch main.cpptouch makefile

Open "makefile" and add the following code to it:

1234567
all: g++ main.cpp -g -o run ./runcompile: g++ main.cpp -g -o runrun: ./run

(If you don't know about makefiles, look at this quick tutorial.)

Notice the extra -g flag for our compile command. This will tell the compiler to leave our code intact in the executable for debugging.

Our C++ program will ask the user for a number, and will calculate the factorial of that number.

​Open "main.cpp" and add the following code to it.

 1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233
// Copyright srcmake.com 2018.// A simple C++ with a bug or two in it.#include <iostream>using namespace std;int main() { // A factorial program to calculate n!. cout << "Enter a number, and we'll calculate the factorial of it.\n"; cout << "Number: "; int n; cin >> n; // The result of n!. Holds the running product of multiplying 1 to n. int factorial; // Go through each number from 1 to n. for(int i = 1; i < n; i++) { factorial = factorial * i; } // Output the result. cout << n << "! is " << factorial << ".\n"; /* Two noteable bugs: 1. "factorial" is never initialized. 2. "factorial" is muliplied from 1 to (n-1). It's not multiplied by n. */ return 0; }

Our code is complete.

​In your terminal, run "make" to test the code out.

make

Obviously, the answer our program outputs is wrong. Why is it wrong? Well, we could inspect the code to find out, but we could also use gdb to see what's happening in our code.

C++ Debugging - How To Use GDB [Code + Command Line Walkthrough] (1)

Using gdb to Debug The Program

We're going to use gdb to debug our program. Let's go through the commands that we need.

First of all, start gdb. (This still start a gdb shell session.)

gdb

​Next, we're going to tell gdb that our executable file (named "run") is the one we want to debug.

file run

Now we're going to set a breakpoint at main. (A breakpoint is basically where we tell our code "stop here so we can look at it".)

break main

Next, we start the program (executable) to see what's going on in the code.

run

We're going to say "next" (or "n") to go through the code line by line (as it's executed, not necessarily as it's written. For example, for loops will be called a lot).

next

At any time, to see the value of a variable, you can use "p variablename".

p factorial

This whole process will look something like this.

C++ Debugging - How To Use GDB [Code + Command Line Walkthrough] (2)

To quit out of the gdb shell sessions.

quit

And that's it. Use gdb to find out what's going on in your code.

Conclusion

In this tutorial, we had a simple C++ program, with a few bugs. To find out what was going on in our code, we used gdb to go through the code line by line to see what the values of variables were and how the code was executing.

We did a basic tutorial, but gdb can be used for much larger examples. To proceed further, familiarize yourself with all of the gdb commands, and use them as you need to for your own program.

Like this content and want more? Feel free tolook around and find another blog post that interests you. You can also contact me through one of the various social media channels.

Twitter:@srcmake
Discord:srcmake#3644
Youtube:srcmake
Twitch:www.twitch.tv/srcmake
​Github:srcmake

C++ Debugging - How To Use GDB [Code + Command Line Walkthrough] (2024)
Top Articles
Latest Posts
Article information

Author: Virgilio Hermann JD

Last Updated:

Views: 5506

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Virgilio Hermann JD

Birthday: 1997-12-21

Address: 6946 Schoen Cove, Sipesshire, MO 55944

Phone: +3763365785260

Job: Accounting Engineer

Hobby: Web surfing, Rafting, Dowsing, Stand-up comedy, Ghost hunting, Swimming, Amateur radio

Introduction: My name is Virgilio Hermann JD, I am a fine, gifted, beautiful, encouraging, kind, talented, zealous person who loves writing and wants to share my knowledge and understanding with you.