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
Issue Week 30 - 2017
How to Watch Indiana Fever vs. Chicago Sky Online Without Cable
Edina Omni Portal
Craigslist St. Paul
Moon Stone Pokemon Heart Gold
Television Archive News Search Service
His Lost Lycan Luna Chapter 5
Aadya Bazaar
Fusion
Flights to Miami (MIA)
The Haunted Drury Hotels of San Antonio’s Riverwalk
Publix 147 Coral Way
Jessica Renee Johnson Update 2023
Craigslist Greenville Craigslist
Busted Newspaper S Randolph County Dirt The Press As Pawns
No Hard Feelings Showtimes Near Cinemark At Harlingen
Tcu Jaggaer
National Weather Service Denver Co Forecast
Star Wars: Héros de la Galaxie - le guide des meilleurs personnages en 2024 - Le Blog Allo Paradise
Walgreens San Pedro And Hildebrand
Danforth's Port Jefferson
Hyvee Workday
‘The Boogeyman’ Review: A Minor But Effectively Nerve-Jangling Stephen King Adaptation
The best brunch spots in Berlin
Cornedbeefapproved
manhattan cars & trucks - by owner - craigslist
Where to eat: the 50 best restaurants in Freiburg im Breisgau
Craigslist Auburn Al
Free Tiktok Likes Compara Smm
Sinai Sdn 2023
How Much Is An Alignment At Costco
Wisconsin Volleyball Team Leaked Uncovered
Mg Char Grill
LEGO Star Wars: Rebuild the Galaxy Review - Latest Animated Special Brings Loads of Fun With An Emotional Twist
Shnvme Com
Maybe Meant To Be Chapter 43
Edict Of Force Poe
Baywatch 2017 123Movies
Craigslist Gigs Wichita Ks
The Transformation Of Vanessa Ray From Childhood To Blue Bloods - Looper
Ksu Sturgis Library
Final Jeopardy July 25 2023
Yogu Cheshire
How Does The Common App Work? A Guide To The Common App
Craigslist Com Panama City Fl
Shipping Container Storage Containers 40'HCs - general for sale - by dealer - craigslist
Natasha Tosini Bikini
Dagelijkse hooikoortsradar: deze pollen zitten nu in de lucht
Abigail Cordova Murder
Unpleasant Realities Nyt
Diamond Spikes Worth Aj
Stone Eater Bike Park
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 5900

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.