在Linux Ubuntu中逐行调试C和C++代码

49

我正在Ubuntu中使用gedit编码并在终端中运行程序。 而在Windows中使用Turboc或netbeans时,我们可以逐行调试代码。 那么在Ubuntu终端中如何进行逐行调试? 或者还有其他选项吗?


这个问题似乎不适合讨论,因为它涉及到Ubuntu操作系统。 - devnull
你可以使用VS Code。我在这里为它制作了教程:https://medium.com/@dcrystalj/debugging-c-c-code-in-visual-studio-code-vsc-f42fde4c418 - Crystal
4个回答

62

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程序示例Here

GDB文档


27

我认为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'

9
您可以使用gdb来完成此操作。
如果尚未安装,请安装gdb。
sudo apt-get install gdb

然后,您可以按照以下方式调试所选择的可执行文件
gdb <executable name>

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

7

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接