Linux 如何调试 C 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2590931/
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-03 19:55:23  来源:igfitidea点击:

How to debug a C program

clinuxdebugginggcc

提问by ambika

I am using Linux, GCCand C.

我正在使用Linux,GCCC.

I have a make file.

我有一个制作文件。

I want to debugmy module. I don't want to debug a single file, I want to debug the whole module.

我想调试我的模块。我不想调试单个文件,我想调试整个模块。

How can I do it?

我该怎么做?

回答by ambika

Compile your code with the -g flag, and then use the gdb debugger. Documentation for gdb is here, but in essence:

使用 -g 标志编译您的代码,然后使用 gdb 调试器。gdb 的文档在这里,但本质上是:

gcc -g -o prog myfile.c another.c

and then:

进而:

gdb prog

If you want a user-friendly GUI for gdb, take a look at DDDor Insight.

如果您想要 gdb 的用户友好 GUI,请查看DDDInsight

回答by Mawg says reinstate Monica

I guess that you are building from the command line.

我猜你是从命令行构建的。

You might want to consider an IDE (Integrated Development Environment), such as KDevelop or Eclipse, etc (hint - Eclipse ... ECLPISE ... E C L I PS E).

您可能需要考虑使用 IDE(集成开发环境),例如 KDevelop 或 Eclipse 等(提示 - Eclipse ... ECLPISE ... ECLI PS E)。

Use an IDE to edit your code, refactor your code, examine your code - class tree, click a variable, class or function to jump to declaration, etc, etc

使用 IDE 编辑代码、重构代码、检查代码 - 类树、单击变量、类或函数以跳转到声明等

And - of course - to debug:

而且 - 当然 - 调试:

  • run your code in the IDE
  • set breakpoints to stop at particular lines
  • or just step through, a line at a time
  • examine the call stack to see how you go there
  • examine the current values of variables, to understand your problem
  • change the values of those variables and run to see what happens
  • and more, more, more
  • 在 IDE 中运行您的代码
  • 设置断点以在特定行停止
  • 或者只是一步一步,一次一行
  • 检查调用堆栈,看看你是如何去那里的
  • 检查变量的当前值,以了解您的问题
  • 更改这些变量的值并运行以查看会发生什么
  • 还有更多,更多,更多

p.s as wasatz mentioned- DDDis great - for visualizing the contents of arrays/matrices, and - imo - especially if you have linked lists

ps 正如 wasatz 提到的 - DDD很棒 - 用于可视化数组/矩阵的内容,以及 - imo - 特别是如果你有链表

回答by Dennis V.R.

You can use gdb-based simple and useful GUI "Nemiver". It can debug your whole module comprising many source files.

您可以使用基于 gdb 的简单实用的 GUI“Nemiver”。它可以调试包含许多源文件的整个模块。

enter image description here

在此处输入图片说明