在Windows上的VSCode中使用GDB进行调试

时间:2020-06-05 20:39:38

标签: c++ windows visual-studio-code gdb

我在这里有点茫然,我真的没想到这会很困难。我通常在Linux上工作,但是今天我需要做一些工作,只有一台Windows机器。我以为这没问题,我可以为Windows安装git,克隆我的项目,然后开始工作。只是一团糟。我真的希望有人可以帮助我了解在Windows上进行所有设置时出错的地方。我不打算经常这样做,但是我绝对希望能够在相当长的时间内在Windows计算机上执行该操作。

我正在使用WSL,并将默认的VSCode Windows集成终端设置为C:\WINDOWS\System32\bash.exe

我安装了Windows 10 SDK来修复crtdbg.h包含错误,作为对<iostream>的依赖性

我用MinGW安装了gdb- MinGW Install Configuration

我设置路径环境变量

enter image description here

我创建了一个launch.json-

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) CDLL Driver",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/driver",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
} 

我的MinGW箱中包含以下

MinGW Bin Contents

我在VSCode中启动调试任务,并收到以下错误消息

cmd /C "c:\Users\shaun\.vscode\extensions\ms-vscode.cpptools-0.28.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-4n4ohh2f.ibt --stdout=Microsoft-MIEngine-Out-1irudlfy.q5x --stderr=Microsoft-MIEngine-Error-fg20cagk.ynl --pid=Microsoft-MIEngine-Pid-kzdzn4p4.lro --dbgExe=C:\MinGW\bin\gdb.exe --interpreter=mi "

Command 'cmd' not found, but there are 16 similar ones.

如有需要,我可以提供更多信息。我真的希望我错过了一些简单的东西,这些东西对于在Windows上工作的人来说是显而易见的。。在此先感谢您,我非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

如果使用WSL编译项目,则不应使用MinGW gdb。 您需要在Linux子系统上安装gdb(如果使用Ubuntu WSL,则使用apt等本机工具),在WSL中重新打开项目,并配置gdb的WSL路径。 我可以在WSL上使用此设置成功调试。

答案 1 :(得分:0)

用这个文件替换你的 launch.json 文件

{
  "version": "0.2.0",
  "configurations": [
    {
      "args": ["1"],
      "name": "gcc.exe - Build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",

      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "C/C++: gcc.exe build active file"
    }
  ]
}

确保你已经安装了 MinGw Compiler 和 gdb debugger