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.
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:
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.
Open "makefile" and add the following code to it:
(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.
Our code is complete.
In your terminal, run "make" to test the code out.
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.
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.)
Next, we're going to tell gdb that our executable file (named "run") is the one we want to debug.
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".)
Next, we start the program (executable) to see what's going on in the code.
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).
At any time, to see the value of a variable, you can use "p variablename".
This whole process will look something like this.
To quit out of the gdb shell sessions.
And that's it. Use gdb to find out what's going on in your code.
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 to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.