问题
I have written a 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?
回答1:
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.
回答2:
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
回答3:
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...)
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/370622/debug-c-program-in-linux