Compiling C++ with Debug Symbols – gdb debugger Tutorial & Examples

g++ gdb: In this article we are going to discuss how to debugging using gdb debugger in C++.

gdb debugger is an application for execute C or C++ program to check the code working and it’s crashes.There are so many task which can be done using gdb debugger,like check prgram,run code,test new conditions and find out errors.

Compile Code with Debug Symbols

We should debug C or C++ program in debug mode.We should use -g option in it.

Below is our first step to debug program using gdb,

g++ -g filename.cpp -o Sample

Now there is two way to debug using gdb debugger;

1.Debugging with executable name,

gdb filename

It will read symbols from ‘filename’ after this type below command;

(gdb) run

2.Start gdb alone without debugging-

gdb

Now give the file name for execution,

(gdb) file filename

After that your application start debugging in debug mode.

Passing Command Line Arguments while debugging

We can also pass command argument at the time of debugging.For this we use below command;

Reading symbols from filename...done.
(gdb) run 1 2 3

There are so many different command in gdb debugger to know them you can run help command,

(gdb) help

It will show you all command list and their working,like below;

Command Description
help Provides information about a topic or commands you can use
file Indicates which file to debug
run Executes the program under GDB
break Adds breakpoints to stop the program execution temporary
delete Deletes breakpoints
clear Deletes breakpoints for a specific function
continue Resets the program after a breakpoint triggers
step Runs the current source line and stops before executing the next one
ext Runs the current source line. However, if the line is a function call, the debugger will fully execute the function before the execution stops again
until Runs the current source line. If you are at the beginning of a loop, the debugger runs until the loop ends
list Prints a certain list from the source code
print Prints values of certain expressions

How do I exit the debugging

For exiting you have to write quit command and you will be out from debug mode,

(gdb) quit

And if you again want to come in debug mode then you will just press “Ctrl-C”.

Conclusion:

In this article we have discussed how to debugging using gdb debugger in C++.Thank you!