vs code c++: unable to establish a connection to GDB - c++

I'm trying to set up a OpenGL environment in vs code, I'm using MinGW64 with msys for compilation and package management, I wrote a tasks and launch json files for generating builds, but when I run the build that was generated I get an error stating "unable to establish connection to GDB" and my app aborts.
this is my launch.json:
"version": "0.2.0",
"configurations":
[
{
"name": "Lauch OpenGL App",
"type": "cppdbg",
"request": "launch",
"preLaunchTask": "Build OpenGL App",
"cwd": "${workspaceRoot}",
"program": "${workspaceRoot}\\Build\\app",
"stopAtEntry": false,
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands":
[
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
This is my tasks.json:
{
"tasks":
[
{
"label": "Compile source code",
"type": "shell",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args":
[
"-c",
"main.cpp",
"-o",
"Build\\Temp\\main.o"
]
},
{
"label": "Link Libraries",
"type": "shell",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args":
[
"-o",
"Build\\app",
"Build\\Temp\\main.o",
"-L.",
"-lglfw3",
"-lopengl32",
"-lgdi32"
]
},
{
"label": "Cleanup",
"type": "shell",
"command": "Remove-Item",
"args":
[
"Build\\Temp\\*.*"
]
},
{
"label": "Build OpenGL App",
"dependsOrder": "sequence",
"dependsOn": ["Compile source code", "Link Libraries", "Cleanup"]
}
],
"version": "2.0.0"
}
When I run my build tasks everything works until the moment the app launches then the following error is shown:
And this is printed to the console:

No clue what the problem was, but I ended up reinstalling everything (msys, mingw, g++, gdb etc....) and the issue was fixed

Related

how can I use vscode on windows to remote debug launch a linux based project writen by c++ and built by makefile?

Background:
I am using vscode on windows to remote debug a linux based server project writen by c++ and built by makefile;
the Binary executable files made by the makefile could be launched by shell or scripts:
./executable_files projects.cfg, and the projects.cfg is a file to tell the project some parameters like port, time_out etc.
task.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "clean",
"type": "shell",
"command": "make",
"args": [
"clean"
],
"problemMatcher": [],
"group": "build"
},
{
"label": "make",
"type": "shell",
"command": "make",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/executable_files",
"args": [
"-c",
"${workspaceFolder}/projects.cfg"
],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
I have already set a breakpoints at the entry of the main function
Problems:
when I hit the F5 to debug this projects, it can not even go into the main function, and the debug function do not work to debug by step
enter image description here
Help:
so how can I fix this probelm?

How to prevent the debugger from running after build failure in VS Code?

I want the debugger not to be started when there are compilation errors in the code. This situation occurs when I have a successful build, then I make some changes that do not compile, and when I am launching the debugger, build fails, but the debugger is still launched with an old executable.
I know about Features -> Debug -> On task errors option, but it does not change this behavior.
I have the latest version of VS Code on Windows, standard C++ extension and mingw.
This is my pretty much standard configuration:
.code-workspace
{
"folders": [
{
"path": ".."
}
],
"settings": {
"debug.onTaskErrors": "abort"
}
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - debug",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe debug build"
},
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe debug build",
"command": "D:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": ["-std=c++17","-Wall","-Wextra","-D","LOCAL","-g","${file}","-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"D:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe\""
},
]
}
What else do I need to do?
I create a script, it delete the binary first, then run compile.
If build fail, new exe will not exists, debugger will fail to start.

VsCode debugging with c++17 fails?

I'm getting an issue where I can build and run a project with c++17 flags enabled, but I can't actually debug it. The debugger launches in vsCode, but all the c++ 17 features are red squiggled, and those lines never execute properly. I'm guessing it's a problem with my launch.json, but I can't figure out what it is that I'm missing...
I'm running windows 10, and here's the relevant configuration in vsCode:
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-std=c++17",
"-g",
"${fileDirname}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task by Debugger"
}
],
"version": "2.0.0"
}
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 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": ["-std=c++17"],
"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": "C/C++: g++.exe build active file"
}
]
}
Welp - not much of an answer, but the issue isn't happening when I just use cl.exe as the compiler instead of gcc. I've since just stopped using gcc for this project after trying to get it to work for about an hour.
https://code.visualstudio.com/docs/cpp/config-msvc

How to setup VSCode debugger to work with cmake?

I found a question here that is pretty close, but the C++ extension has been updated since then and the launch.json file that is created for you is very different and I'm not sure how to set it up. There also isn't a lot of info online to do this, so I'm hoping someone has done this before. My current project structure is like this:
MainProj
./build
CMakeLists.txt
main.cpp
otherClasses.hpp
And the cmake target that shows up in build after running ..cmake and make is called Main.
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++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
This is the tasks.json file from the other question
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "cd build && cmake .. && make"
}
]
}

configure launch.json file everytime when working with new c++ file in vscode

I am working with c++ in vscode. I have to configure launch.json file everytime I create and work with a new cpp file. Otherwise, it executes the previous file which I worked on.
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/nxtPrime.cpp.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"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": "echo"
}
]
}
Is there any simple way so that I need not edit launch.json file to give the name of new cpp file every time??
Thanks in advance.
I'm using VS Code writing C# programs who are kind of relatable to C++
I also chose the Framework "net5.0" and below, nothing of .NET Core even if that name appears in the config.
When you create a new project two files are created on the folder .vscode the launch.json and task.json, the first one is for you, just tweak it a little in the path.
For launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/${relativeFileDirname}/bin/Debug/net5.0/${relativeFileDirname}.dll",
"args": [],
"cwd": "${workspaceFolder}/${relativeFileDirname}",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
For the task.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/${relativeFileDirname}/${relativeFileDirname}.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/${workspaceFolderBasename}/${workspaceFolderBasename}.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/${workspaceFolderBasename}/${workspaceFolderBasename}.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
I tested it using the variable references from the page up to date 2022-04-06 that #Mark recommended above: https://code.visualstudio.com/docs/editor/variables-reference