如何使用MinGW gdb调试器调试Windows中的C ++程序? [英] How do I use the MinGW gdb debugger to debug a C++ program in Windows?

查看:163
本文介绍了如何使用MinGW gdb调试器调试Windows中的C ++程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到关于这个文件,没有找到任何东西。我有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

gdb myprog.exe

几个命令让你开始:


  • break main 将导致调试器在调用 main 时中断。您还可以使用 break FILENAME:LINENO 中断代码行。例如,当程序到达 mycode.cpp 的第4行时, break mycode.cpp:4 li>
  • start 启动程序。

  • break main will cause the debugger to break when main is called. You can also break on lines of code with break FILENAME:LINENO. For example, break mycode.cpp:4 breaks execution whenever the program reaches line 4 of mycode.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 变量。

  • 步骤
  • 下一页 adv +1 前进到下一行。您还可以前进到特定文件的特定行,例如 adv mycode.cpp:8

  • bt 打印回溯。这实质上是一个堆栈跟踪。

  • continue 完全像一个可视化调试器的它会导致程序继续执行,直到下一个断点或程序退出。

  • print VARNAME. That's how you print values of variables, whether local, static, or global. For example, at the for loop, you can type print temp to print out the value of the temp variable.
  • step This is equivalent to "step into".
  • next or adv +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 ++程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆