如何使用 MinGW gdb 调试器在 Windows 中调试 C++ 程序?

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

How do I use the MinGW gdb debugger to debug a C++ program in Windows?

c++windowsdebugginggdbmingw

提问by Mike Webb

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.

我已经查找了有关此的文档,但一无所获。我安装了 MinGW,效果很好。我只是不知道如何使用调试器。

Given some simple code, say in a file called "mycode.cpp":

给定一些简单的代码,在一个名为“mycode.cpp”的文件中说:

int main()
{
    int temp = 0;

    for (int i = 0; i < 5; ++i)
        temp += i;

    return 0;
}

...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?

...我将如何调试这个。我在 Windows 中使用 MinGW 和 GDB 调试代码的命令是什么?我可以像在 Visual Studio 中一样通过命令行单步执行代码吗?如果是这样,我使用什么命令来做到这一点?

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::coutstatements to debug complex code.

是否有使用 GDB 的教程?我找不到任何东西,但如果有人能指引我找到一个,那也太棒了。我厌倦了编写大量std::cout语句来调试复杂的代码。

回答by Daniel Trebbien

The first step is to compile your program with -gto include debugging information within the executable:

第一步是编译你的程序,-g在可执行文件中包含调试信息:

g++ -g -o myprog.exe mycode.cpp

Then the program can be loaded into gdb:

然后程序可以加载到gdb

gdb myprog.exe

A few commands to get you started:

一些让你开始的命令:

  • break mainwill cause the debugger to break when mainis called. You can also break on lines of code with break FILENAME:LINENO. For example, break mycode.cpp:4breaks execution whenever the program reaches line 4 of mycode.cpp.
  • startstarts the program. In your case, you need to set breakpoints before starting the program because it exits quickly.
  • break main将导致调试器在main调用时中断。您还可以使用break FILENAME:LINENO. 例如,break mycode.cpp:4当程序到达第 4 行时中断执行mycode.cpp
  • start启动程序。在您的情况下,您需要在启动程序之前设置断点,因为它会快速退出。

At a breakpoint:

在断点处:

  • print VARNAME. That's how you print values of variables, whether local, static, or global. For example, at the forloop, you can type print tempto print out the value of the tempvariable.
  • stepThis is equivalent to "step into".
  • nextor adv +1Advance 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.
  • btPrint a backtrace. This is a stack trace, essentially.
  • continueExactly like a "continue" operation of a visual debugger. It causes the program execution to continue until the next break point or the program exits.
  • print VARNAME. 这就是您打印变量值的方式,无论是局部的、静态的还是全局的。例如,在for循环中,您可以键入print temp以打印出temp变量的值。
  • step这相当于“踏入”。
  • nextadv +1前进到下一行(如“跨步”)。您还可以前进到特定文件的特定行,例如,adv mycode.cpp:8.
  • bt打印回溯。本质上,这是一个堆栈跟踪。
  • continue就像可视化调试器的“继续”操作一样。它使程序继续执行,直到下一个断点或程序退出。

The best thing to read is the GDB users' manual.

最好阅读GDB 用户手册

回答by Martin Beckett

There are a few gdb guis for windows in this question windows version of the GDB frontend DDD

在这个问题windows version of the GDB frontend DDD中有一些windows的gdb guis

Although DDD hasn't been ported

虽然 DDD 还没有移植