Type: Debugger
(I'm sorry for my poor English)
I want to debug with the built-in terminal on VS Code, so I set "externalConsole": false in launch.json.
But when I started debugging, it told me "Unable to establish a connection to GDB". I have already installed MinGW-w64.

This is my launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Debugging with GDB",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Compile C/C++"
}
]
}
and tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile C/C++",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe",
"-g",
"-Wall",
"-lm",
"-std=c++17"
],
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared"
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": "absolute",
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
Steps to reproduce the behavior:
This error only happens when "externalConsole": false in launch.json. If "externalConsole": true, it will be normal.
@tnqzh123 is there anything in the debug output like the error message says? Can you enable engineLogging and let me know what the output is?
@pieandcakes There are nothing in the debug console, even after I added
"logging": {
"trace": true,
"traceResponse": true,
"engineLogging": true
}
into launch.json.
Now my launch.json is:
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Debugging with GDB",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Compile C/C++",
"logging": {
"trace": true,
"traceResponse": true,
"engineLogging": true
}
}
]
}
I found it will start debugging normally some times when I set "externalConsole": false.
(Only some times!)
I was setting-up vscode for a colleague (i.e, on Windows 7), using the configuration below. At that time it work, few days later it stopped working with the same message as you indicated. It works perfect on my computer (i.e., Windows 10).
I will investigate this further and write back. If you find anything new, please share below.
Configuration on which it works:
CMake -> the issue still persists after uninstalling it Configuration(s) on which it fails:
launch.json configuration:
{
"version": "0.2.0",
"configurations": [
{
"name": "build & debug",
"type": "cppdbg",
"request": "launch",
"preLaunchTask": "build (cmake)",
"program": "${workspaceFolder}/bin/vscode/program.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\CLI\\gcc-tools\\mingw-w64\\x86_64-8.1.0-posix-sjlj-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"logging": {
"engineLogging": true
}
}
]
}
I found it will start debugging normally some times when I set
"externalConsole": false.
(Only some times!)
It seems like the incompatible of windows' power shell, today I change the defualt shell to cmd.exe, and it works.
@tnqzh123 @mihaiconstantin
Thank you! simple but, very powerful solution! @mahonelee
I had the same issue and I'm using PowerShell as my default terminal too.
I found that sometimes there are multiple terminals open (spawned by the debugger sessions) and if one of these sessions didn't terminate properly (for whatever reason), vscode/extension can't establish a new connection to gdb, and launching a new debugging session will fail.
Solution in this case: Kill the freezed terminal (it should have the name "cppdbg:" or "cppvsdbg:" followed by <debugged exe>). If uncertain which one kill 'em all
P.S, Kill terminal is the trash can icon not the x icon.
Thank you for reporting an issue. We do not have enough information at this time to act upon this issue. Please try our latest version and if it is still occuring, provide more information including configurations, logs and version information and reopen this issue.
I ran into the same problem today after updating Windows 10.
As others mentioned above, one workaround is to use cmd.exe instead of PowerShell as an internal shell by adding "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe" to the .vscode/settings.json file.
Another thing that worked for me was uninstalling and reinstalling the C/C++ extension in VS Code.
Also having this error. Thx @christian-vorhemus for solution.
My sys info:
VSCode: 1.41.1
C/C++ 0.26.3
MinGW-W64-builds-4.3.5
Windows 10: Windows 10 Pro 10.0.18363 / 18363
In my case I found that short wait time in MIEngine is causing timeout. If powershell terminal loading takes more than 5 seconds, connection timeouts.
Today when I am programing in Java with VSC, I set "java.debug.settings.console": "externalTerminal" in settings.json. But it failed to debug. So I turn it back into "integratedTerminal". According to this, I want to know if C program can also run in the integrated terminal. So I tried just as assignees does, but failed again. Later, however, I adapted @mahonelee 's opinion and succeeded. So, thank you. But I still have no idea why Java can't run in external terminal, and why C run in external terminal better. I'd appreciated it if someone could help me.
@Infinite-U this is the page for the C/C++ extension. It sounds like you need to ask the question on the Java extension's page.
@Epikem the change for the timeout is in the next release.
Fixed in our latest Insiders release: https://github.com/microsoft/vscode-cpptools/releases/tag/0.27.0-insiders.
If you prefer using git-bash in windows, you can try to install the latest version (https://git-scm.com/download/win) and vscode. Reload vscode and you can debug with the bash terminal.
Most helpful comment