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

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.

[showads ad=inside_post]
To build with debug info we should use -g option i.e.

g++ -g Sample.cpp -o Sample

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

gdb Sample

It will give a prompt like this,
Reading symbols from Sample...done.

Then type run i.e.

Reading symbols from Sample…done.

(gdb) run

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.
(gdb) file Sample

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.

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

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.
(gdb) help


How do I exit the debugging

Just write the “quit” command on gdb prompt.

(gdb) quit

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top