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.
To build with debug info we should use -g option i.e.
1 |
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
1 |
gdb Sample |
It will give a prompt like this,
1 2 |
Reading symbols from Sample...done. (gdb) |
Then type run i.e.
Reading symbols from Sample…done.
1 |
(gdb) run |
2.) Start gdb alone i.e.
gdb
Now it will start the gdb and give a prompt i.e.
1 |
(gdb) |
Now give the executable name along with file command i.e.
1 |
(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.
1 2 |
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.
1 |
(gdb) help |
How do I exit the debugging
Just write the “quit” command on gdb prompt.
1 |
(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.
Python Resources
- Best Python Tutorials on lists, dict, functions, iterators & many more.
- Data Analysis in Python using Pandas Dataframe - Top Tutorials
C++11 / C++14 Resources
- Best C++11 Tutorials on Topics like Smart Pointers, tuples, Unordered map / set, lambda function etc.
- C++11 Multi-threading Series
- C++ - Boost Library Tutorials
Leave a Reply