Line by line c - c++ code debugging in Linux ubuntu [closed]

I am coding using gedit in ubuntu and running program in terminal. While working in windows using Turboc or netbeans we can debug code line by line. How can we do it in ubuntu terminal? or any other option?


Solution 1:

gdb (The Gnu debugger) is best choice

apt-get install gdb

man gdb

1.    cc -g file.c             //       compile your program ,this will generate a.out file with required debugging information 

2.    gdb a.out                //        start with gdb

3.    b main                   //        to set break point at main       

4.     run                     //        run now , and it will stop at break point main 

5.     s                       //        option s is to step single line and even step into functions

6.     n                       //        option n is to execute next line and step over functions  

7.     p    variable name      //        to print the value of variable at that particular instance very helpful  

man gdb will give more info

All useful gdb commands and an example with simple cpp program are given Here

GDB Documentation

Solution 2:

I find GDB (Gnu DeBugger) to be the best tool for c/c++. It's probably already installed on your system if you have gcc installed.

To use it, make sure you compile your program with the -g flag:

gcc -g myprog.c -o myprog

And then launch the debugger with

gdb ./myprog

Here are some basic commands to get you going:

b lineno           - set a break point at line 'lineno'
b srcfile:lineno   - set a break point in source file 'srcfile' at line 'lineno'
r                  - run the program
s                  - step through the next line of code
c                  - continue execution up to the next breakpoint
p varname          - print the value of the variable 'varname'

Solution 3:

You can use gdb for this.

Install gdb if it isn't already installed.

sudo apt-get install gdb

Then you can debug the executable of choice as follows

gdb <executable name>

You get a complete interactive debug session.

Solution 4:

You can use an IDE(http://en.wikipedia.org/wiki/Integrated_development_environment) which provides code management, highlighting, debugging facilities. You may try any of these.

  • QTCreator(http://qt-project.org/wiki/Category:Tools::QtCreator)
  • KDevelop(http://www.kdevelop.org/)
  • Eclipse(http://www.eclipse.org/)

or you may choose to use gdb(https://www.gnu.org/software/gdb/) directly from the command line.