In this article we will discuss how to build a C++ / C code in debug mode and how to start the debugging using gdb.

Compile Code with Debug Symbols

To debug a C or C++ application with gdb, it should be built in debug mode. So that debug symbols can be loaded and used by gdb.

To build with debug info we should use -g option i.e.

Now executable “Sample” can be debug with gdb.

How to Start Debugging with gdb

There are 2 ways to start debugging using gdb i.e

1.) Start the debugging with executable name i.e

It will give a prompt like this,

Then type run i.e.

Reading symbols from Sample…done.

2.) Start gdb alone i.e.

Now it will start the gdb and give a prompt i.e.

Now give the executable name along with file command i.e.

Now, your application will start in debug Mode.

Passing Command Line Arguments while debugging

To pass the command line arguments while starting the application in debug mode, just pass the argument list to run command i.e.

gdb has an interactive shell where you can run different commands to achieve many things. To get the help about these commands use help command i.e.


How do I exit the debugging

Just write the “quit” command on gdb prompt.

To place the breakepoints and other stuff you need to execute commands on “gdb” prompt. To get the gdb prompt back in application started with through gdb just press “Ctrl-C”.

We will discuss regarding breakpoints in next article.

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.