首页 > 解决方案 > 在 VSCode 上使用 F5 进行调试失败,提示“无法建立与 GDB 的连接”

问题描述

当我按 F5 键在 Visual Studio Code 上运行 C++ 程序时,我收到消息

"Unable to start debugging. Unable to establish a connection to GDB. Debug output may contain more information."

我使用g++.exe和 MinGW 进行编译。直到一个小时前,用 F5 键调试正常,我从来没有碰过launch.json. 你能告诉我有什么问题吗?

下面是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": "g++.exe - アクティブ ファイルのビルドとデバッグ",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "externalTerminal",
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "gdb の再フォーマットを有効にする",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe アクティブなファイルのビルド"
        }
    ]
}

下面是tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe アクティブなファイルのビルド",
            "command": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "コンパイラ: C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe アクティブなファイルのビルド ver(1)",
            "command": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "デバッガーによって生成されたタスク。"
        }
    ]
}

下面是c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${vcpkgRoot}/x86-windows-static/include",
                "C:\\Users\\SonicTheHedgehog\\Desktop\\include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

补充:我发现了一条错误信息launch.json

Property console is not allowed

console标有波浪线。

标签: c++visual-studio-codegdbg++mingw

解决方案


查看此链接:https ://github.com/microsoft/vscode-cpptools/issues/2889

PowerShell 在内部控制台中运行(在 VSCode 中运行)存在一些问题。

我在 launch.json 中做了以下更改:

前:

        "environment": [],
        "externalConsole": **false**,
        "MIMode": "gdb",

后:

        "environment": [],
        "externalConsole": **true**,
        "MIMode": "gdb",

现在,我在 VSCode 之外的 cmd.exe 中看到了程序的输出。但是,调试器在 VSCode 中运行良好。


推荐阅读