問題描述
我已經查找了有關此的文檔,但一無所獲.我安裝了 MinGW,效果很好.我只是不知道如何使用調試器.
I have looked for documentation on this and found nothing. I have MinGW installed and it works great. I just don't know how to use the debugger.
給定一些簡單的代碼,比如在一個名為mycode.cpp"的文件中:
Given some simple code, say in a file called "mycode.cpp":
int main()
{
int temp = 0;
for (int i = 0; i < 5; ++i)
temp += i;
return 0;
}
...我將如何調試它.我在 Windows 中使用 MinGW 和 GDB 調試代碼的命令是什么?我可以像在 Visual Studio 中一樣通過命令行單步執行代碼嗎?如果是這樣,我使用什么命令來做到這一點?
...how would I debug this. What are the commands that I use to debug code with MinGW and GDB in windows? Can I step through the code via the command line like in Visual Studio? If so what commands do I use to do that?
是否有使用 GDB 的教程?我找不到任何東西,但如果有人能指導我找到一個,那也太棒了.我厭倦了編寫大量 std::cout
語句來調試復雜代碼.
Are there any tutorials for using GDB out there? I couldn't find any, but if anyone could direct me to one that would be great too. I'm tired of writing tons of std::cout
statements to debug complex code.
推薦答案
第一步是使用 -g
編譯您的程序,以在可執行文件中包含調試信息:
The first step is to compile your program with -g
to include debugging information within the executable:
g++ -g -o myprog.exe mycode.cpp
然后程序就可以加載到gdb
中:
Then the program can be loaded into gdb
:
gdb myprog.exe
一些讓您入門的命令:
break main
將在調用main
時導致調試器中斷.您還可以使用break FILENAME:LINENO
中斷代碼行.例如,只要程序到達mycode.cpp
的第 4 行,break mycode.cpp:4
就會中斷執行.start
啟動程序.在您的情況下,您需要在啟動程序之前設置斷點,因為它會快速退出.
break main
will cause the debugger to break whenmain
is called. You can also break on lines of code withbreak FILENAME:LINENO
. For example,break mycode.cpp:4
breaks execution whenever the program reaches line 4 ofmycode.cpp
.start
starts the program. In your case, you need to set breakpoints before starting the program because it exits quickly.
在斷點處:
打印 VARNAME
.這就是您打印變量值的方式,無論是局部的、靜態的還是全局的.例如,在for
循環中,您可以鍵入print temp
以打印出temp
變量的值.step
這相當于步入".next
或adv +1
前進到下一行(如step over").您還可以使用例如adv mycode.cpp:8
前進到特定文件的特定行.bt
打印回溯.這本質上是一個堆棧跟蹤.continue
與可視化調試器的繼續"操作完全一樣.它使程序繼續執行,直到下一個斷點或程序退出.
print VARNAME
. That's how you print values of variables, whether local, static, or global. For example, at thefor
loop, you can typeprint temp
to print out the value of thetemp
variable.step
This is equivalent to "step into".next
oradv +1
Advance to the next line (like "step over"). You can also advance to a specific line of a specific file with, for example,adv mycode.cpp:8
.bt
Print a backtrace. This is a stack trace, essentially.continue
Exactly like a "continue" operation of a visual debugger. It causes the program execution to continue until the next break point or the program exits.
最好閱讀GDB 用戶手冊.
這篇關于如何使用 MinGW gdb 調試器在 Windows 中調試 C++ 程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!