Linux ubuntu中逐行c-c++代码调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18271363/
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
Line by line c - c++ code debugging in Linux ubuntu
提问by user123
I am coding using gedit in ubuntu and running program in terminal. While working in windows using Turboc or netbeans we can debug code line by line. How can we do it in ubuntu terminal? or any other option?
我在 ubuntu 中使用 gedit 进行编码并在终端中运行程序。在使用 Turboc 或 netbeans 在 windows 中工作时,我们可以逐行调试代码。我们如何在 ubuntu 终端中做到这一点?或任何其他选择?
回答by Gangadhar
gdb(The Gnu debugger) is best choice
gdb(Gnu 调试器)是最佳选择
apt-get install gdb
apt-get 安装 gdb
man gdb
男人 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 gdbwill give more info
man gdb会提供更多信息
All useful gdb commands and an example with simple cpp program are given Here
所有有用的gdb命令,并用简单的CPP程序为例,给出这里
回答by AlexJ136
I find GDB (Gnu DeBugger) to be the best tool for c/c++. It's probably already installed on your system if you have gcc installed.
我发现 GDB (Gnu DeBugger) 是 c/c++ 的最佳工具。如果您安装了 gcc,它可能已经安装在您的系统上。
To use it, make sure you compile your program with the -g
flag:
要使用它,请确保使用以下-g
标志编译您的程序:
gcc -g myprog.c -o myprog
And then launch the debugger with
然后启动调试器
gdb ./myprog
Here are some basic commands to get you going:
以下是一些基本命令,可帮助您前进:
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'
回答by amrith
You can use gdb for this.
您可以为此使用 gdb。
Install gdb if it isn't already installed.
如果尚未安装 gdb,请安装它。
sudo apt-get install gdb
Then you can debug the executable of choice as follows
然后你可以调试选择的可执行文件如下
gdb <executable name>
You get a complete interactive debug session.
您将获得完整的交互式调试会话。
回答by HAL
You can use an IDE(http://en.wikipedia.org/wiki/Integrated_development_environment) which provides code management, highlighting, debugging facilities. You may try any of these.
您可以使用提供代码管理、突出显示和调试功能的 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/)
QTCreator
( http://qt-project.org/wiki/Category:Tools::QtCreator)KDevelop
( http://www.kdevelop.org/)Eclipse
( http://www.eclipse.org/)
or you may choose to use gdb
(https://www.gnu.org/software/gdb/) directly from the command line.
或者您可以选择直接从命令行使用gdb
(https://www.gnu.org/software/gdb/)。