Edit: I was using Microsoft's C/C++ plugin for vscode and you may have luck getting it to work by following this guide for the json setup. I haven't tried it out yet, but it might be able to help you get it working on windows. I will try to post an answer if I figure it out and have time, but otherwise feel free to answer and if yours works I'll accept and upvote it. Also maybe try changing the value of this "key": "value"
pair "miDebuggerPath": "C:\\rhcygwin64\\bin\\gdb.exe"
, to the window's path of Cygwin's output of which gdb
.
Question
I've been trying to get C++ building and debugging working in visual studio code on Windows. Maybe I'm going about this the wrong way. I have downloaded g++ through cygwin and added it to my path. My tasks.json
file looks like this.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"args": ["-g", "main.cpp"],
"showOutput": "always"
}
I run this task and it seems to build the executable with no problems. The problem is in the next step getting it to launch the debugger. My launch.json
looks like so:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [
{"name":"PYTHONHOME", "value": "/usr/"},
{"name":"PYTHONPATH", "value": "/usr/lib/python2.7"}
],
"externalConsole": true,
"miDebuggerPath": "C:\\rhcygwin64\\bin\\gdb.exe",
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
},
{
"name": "C++ Attach",
"type": "cppdbg",
"request": "attach",
"program": "enter program name, for example ${workspaceRoot}/a.out",
"processId": "${command.pickProcess}",
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
}
]
}
Then I get the following error when I launch the debugger:
Starting: "C:\rhcygwin64\bin\gdb.exe" --interpreter=mi
ImportError: No module named site
"C:\rhcygwin64\bin\gdb.exe" exited with code 1 (0x1).
I was able to reproduce the error in a command prompt and when I set the environment variable for PYTHONHOME
and PYTHONPATH
for cygwin gdb was able to run successfully(like in this answer: https://stackoverflow.com/a/19377110/2066736). So that's why in my launch.json
I set the environment variables like so:
"environment": [
{"name":"PYTHONHOME", "value": "/usr/"},
{"name":"PYTHONPATH", "value": "/usr/lib/python2.7"}
],
But it's still giving me the error. I feel it is giving me this error because it is only planning on running the executable with the environment variables and not the gdb command. Any help on getting this thing to debug with gdb?