在Linux ubuntu中逐行调试c-c++代码

Line by line c - c++ code debugging in Linux ubuntu

本文关键字:调试 c-c++ 代码 逐行 Linux ubuntu      更新时间:2023-10-16

我在ubuntu中使用gedit进行编码,并在终端中运行程序。在使用Turboc或netbeans的窗口中工作时,我们可以逐行调试代码。我们如何在ubuntu终端中做到这一点?还是其他选择?

gdb(Gnu调试器)是的最佳选择

apt-get-install gdb

man gdb

1.    cc -g file.c             //       compile your program ,this will generate a.out file with required debugging information 
2.    gdb a.out                //        start with gdb
3.    b main                   //        to set break point at main       
4.     run                     //        run now , and it will stop at break point main 
5.     s                       //        option s is to step single line and even step into functions
6.     n                       //        option n is to execute next line and step over functions  
7.     p    variable name      //        to print the value of variable at that particular instance very helpful  

man gdb将提供更多信息

这里给出了所有有用的gdb命令和一个带有简单cpp程序的示例

GDB文档

我发现GDB(Gnu DeBugger)是c/c++的最佳工具。如果你安装了gcc,它可能已经安装在你的系统上了。

要使用它,请确保使用-g标志编译程序:

gcc -g myprog.c -o myprog

然后用启动调试器

gdb ./myprog

这里有一些基本的命令让你开始:

b lineno           - set a break point at line 'lineno'
b srcfile:lineno   - set a break point in source file 'srcfile' at line 'lineno'
r                  - run the program
s                  - step through the next line of code
c                  - continue execution up to the next breakpoint
p varname          - print the value of the variable 'varname'

您可以使用gdb。

如果尚未安装gdb,请安装它。

sudo apt-get install gdb

然后你可以调试选择的可执行文件如下

gdb <executable name>

您将获得一个完整的交互式调试会话。

您可以使用IDE(http://en.wikipedia.org/wiki/Integrated_development_environment)它提供代码管理、突出显示和调试功能。你可以试试这些。

  • QTCreator(http://qt-project.org/wiki/Category:Tools::QtCreator)
  • KDevelop(http://www.kdevelop.org/)
  • Eclipse(http://www.eclipse.org/)

或者您可以选择使用gdb(https://www.gnu.org/software/gdb/)直接从命令行。