当我按 F5 键在 Visual Studio Code 上运行 C++ 程序时,我收到消息
“无法开始调试。无法与 GDB 建立连接。调试输出可能包含更多信息。”
我使用 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
不允许属性控制台
和console
用波浪线标记。
最佳答案
查看此链接:https://github.com/microsoft/vscode-cpptools/issues/2889
在内部控制台(在 VSCode 中运行)中运行的 PowerShell 存在一些问题。
我在 launch.json 中做了以下更改:
之前:
"environment": [],
"externalConsole": **false**,
"MIMode": "gdb",
之后:
"environment": [],
"externalConsole": **true**,
"MIMode": "gdb",
现在,我在 VSCode 外的 cmd.exe 中看到程序的输出。但是,调试器在 VSCode 中运行良好。
关于c++ - 在 VSCode 上使用 F5 调试失败,提示 "Unable to establish a connection to GDB",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66365840/