简体   繁体   English

在Linux中调试C ++程序

[英]Debug C++ program in Linux

I have written a simple C++ program like this: 我写了一个简单的C ++程序,如下所示:

#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? 那么它的命令是什么,所以我的控制权到达每一行?

You can use gdb for this: 您可以使用gdb

$ gdb hello

This will start gdb and prompt you for what to do next. 这将启动gdb并提示您下一步该做什么。 The next command executes one line of source and stops at the next line. next命令执行一行源并在下一行停止。

I found a basic GDB tutorial that may be helpful. 我发现了一个可能有用的基本GDB教程

Don't forget to compile your source code using -g option. 不要忘记使用-g选项编译源代码。 Like this: g++ -g helloWorld.cc This is going to create an a.out executable file. 像这样: g++ -g helloWorld.cc这将创建一个a.out可执行文件。 You'll be able to debug your a.out exe using gdb ./a.out command. 您将能够使用gdb ./a.out命令调试a.out exe。 Another tool you may use it's ddd basically a GUI for gdb. 您可以使用它的另一个工具是ddd,基本上是gdb的GUI。

Good luck 祝好运

I always thought emacs provided a pretty user-friendly front-end to gdb... 我一直认为emacs为gdb提供了一个非常友好的前端...

Eg 例如

  • % g++ hello.cc -g -o hello %g ++ hello.cc -g -o hello
  • emacs hello.cc emacs hello.cc
  • [ In Emacs ] Escape-x gdb [ 在Emacs中 ] Escape-x gdb
  • Emacs will say "Run gdb (like this): gdb ". Emacs会说“运行gdb(像这样):gdb”。
  • Add your binary name ("hello"). 添加二进制名称(“hello”)。 (Eg "Run gdb (like this): gdb hello".) (例如“运行gdb(像这样):gdb hello”。)
  • Go to your hello.cc buffer. 转到hello.cc缓冲区。
  • Use the emacs command 'gud-break' to set a breakpoint in gdb from your hello.cc buffer. 使用emacs命令'gud-break'在hello.cc缓冲区中设置gdb中的断点。 (Normally bound to "Cx space".) (通常绑定到“Cx空间”。)
  • Go to your *gud-hello* buffer. 转到你的* gud-hello *缓冲区。
  • Type "run" at the (gdb) prompt. 在(gdb)提示符下键入“run”。
  • Use [ N ] Next or [ S ] Step. 使用[ N ] Next或[ S ] Step。 Or [ C ] Continue. 或者[ C ]继续。 [ BT ] Backtrace is also useful. [ BT ] Backtrace也很有用。
  • Note what happens to the little triangle in the leftmost column of your hello.cc buffer. 请注意hello.cc缓冲区最左侧列中的小三角形会发生什么。

(That should suffice to get you started. Emacs being emacs, there are always more features...) (这应该足以让你开始.Emacs是emacs,总有更多的功能...)

If you want some user-friendly debugger, you can use Kdbg, which is basically a gdb frontend for KDE. 如果你想要一些用户友好的调试器,你可以使用Kdbg,它基本上是KDE的gdb前端。 Perhaps not so powerful as ddd, but easier to start with. 也许没有ddd那么强大,但更容易入手。

In the C++ Programming course I did in Sweden there was a part of the laboratory about the GNU Debugger. 在我在瑞典做的C ++编程课程中,有一部分实验室关于GNU调试器。 I never used it after, but here there is a paper explaining the basic usage, as far as I remember is in chapter 2. 之后我从未使用它,但是这里有一篇论文解释了基本用法,据我记得在第2章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM