在 Linux 中调试 C++ 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/370622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:00:54  来源:igfitidea点击:

Debug C++ program in Linux

c++linuxdebugging

提问by Greg Hewgill

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?

现在我想调试它。那么它的命令是什么,这样我的控制就到每一行?

回答by Greg Hewgill

You can use gdbfor this:

您可以gdb为此使用:

$ gdb hello

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

这将启动gdb并提示您下一步要做什么。该next命令执行一行源代码并在下一行停止。

I found a basic GDB tutorialthat may be helpful.

我找到了一个可能有帮助的基本GDB 教程

回答by dario minonne

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

不要忘记使用 -g 选项编译源代码。像这样:g++ -g helloWorld.cc这将创建一个 a.out 可执行文件。您将能够使用gdb ./a.out命令调试 a.out exe 。您可能会使用的另一个工具是ddd,它基本上是 gdb 的 GUI。

Good luck

祝你好运

回答by Mr.Ree

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

我一直认为emacs为 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.
  • % g++ hello.cc -g -o 你好
  • emacs hello.cc
  • [在 Emacs 中] Escape-x gdb
  • Emacs 会说“运行 gdb(像这样):gdb”。
  • 添加您的二进制名称(“hello”)。(例如“运行 gdb(像这样):gdb hello”。)
  • 转到您的 hello.cc 缓冲区。
  • 使用 emacs 命令 'gud-break' 从 hello.cc 缓冲区在 gdb 中设置断点。(通常绑定到“Cx 空间”。)
  • 转到您的 *gud-hello* 缓冲区。
  • 在 (gdb) 提示符下键入“run”。
  • 使用 [ N] Next 或 [ S] Step。或 [ C] 继续。[ BT] 回溯也很有用。
  • 请注意 hello.cc 缓冲区最左侧列中的小三角形发生了什么。

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

(这应该足以让你开始。Emacs 就是 emacs,总是有更多的功能......)

回答by tunnuz

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 herethere is a paper explaining the basic usage, as far as I remember is in chapter 2.

在我在瑞典上的 C++ 编程课程中,有一部分是关于 GNU 调试器的实验室。之后我再也没有使用过它,但这里有一篇解释基本用法的论文,据我记得是在第 2 章。

回答by Marc

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.

如果你想要一些用户友好的调试器,你可以使用 Kdbg,它基本上是 KDE 的 gdb 前端。也许不如 ddd 强大,但更容易上手。