views:

985

answers:

5

I have written simple C++ program like this:

#include <iostream>
using namespace std;
int main()
{
cout << "Hello.";
return 0;
}

now I want to debug it. so what will be the command for it so my control goes to every line. Plz help me.

+15  A: 

You can use gdb for this:

$ gdb hello

This will start gdb and prompt you for what to do next. The next command executes one line of source and stops at the next line.

I found a basic GDB tutorial that may be helpful.

Greg Hewgill
+10  A: 

Don't forget to compile your source code using -g option. Like this: g++ -g helloWorld.cc This is going to create an a.out executable file. You'll be able to debug your a.out exe using gdb ./a.out command. Another tool you may use it's ddd basically a GUI for gdb.

Good luck

dario minonne
+2  A: 

If you want some user-friendly debugger, you can use Kdbg, which is basically a gdb frontend for KDE. Perhaps not so powerful as ddd, but easier to start with.

Marc
+3  A: 

I always thought emacs provided a pretty user-friendly front-end to gdb...

E.g.

  • % g++ hello.cc -g -o hello
  • emacs hello.cc
  • [ In Emacs ] Escape-x gdb
  • Emacs will say "Run gdb (like this): gdb ".
  • Add your binary name ("hello"). (E.g. "Run gdb (like this): gdb hello".)
  • Go to your hello.cc buffer.
  • Use the emacs command 'gud-break' to set a breakpoint in gdb from your hello.cc buffer. (Normally bound to "C-x space".)
  • Go to your *gud-hello* buffer.
  • Type "run" at the (gdb) prompt.
  • Use [ N ] Next or [ S ] Step. Or [ C ] Continue. [ BT ] Backtrace is also useful.
  • Note what happens to the little triangle in the leftmost column of your hello.cc buffer.

(That should suffice to get you started. Emacs being emacs, there are always more features...)

Mr.Ree
A: 

In the C++ Programming course I did in Sweden there was a part of the laboratory about the GNU Debugger. I never used it after, but here there is a paper explaining the basic usage, as far as I remember is in chapter 2.

tunnuz