Setting up Visual Studio Code to debug with gdb on cygwin
<p><strong>Edit</strong>: I was using <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools" rel="nofollow noreferrer">Microsoft's C/C++ plugin</a> for vscode and you may have luck getting it to work by following <a href="https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md" rel="nofollow noreferrer">this guide for the json setup</a>. 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 <code>"key": "value"</code> pair <code>"miDebuggerPath": "C:\\rhcygwin64\\bin\\gdb.exe"</code>, to the window's path of Cygwin's output of <code>which gdb</code>.</p>

<hr>

<p><strong>Question</strong></p>

<p>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 <code>tasks.json</code> file looks like this.</p>

<pre><code>{
    // 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"
}
</code></pre>

<p>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 <code>launch.json</code> looks like so:</p>

<pre><code>{
    "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
                    }
                ]
            }
        }
    ]
}
</code></pre>

<p>Then I get the following error when I launch the debugger:</p>

<pre><code>Starting: "C:\rhcygwin64\bin\gdb.exe" --interpreter=mi
ImportError: No module named site
"C:\rhcygwin64\bin\gdb.exe" exited with code 1 (0x1).
</code></pre>

<p>I was able to reproduce the error in a command prompt and when I set the environment variable for <code>PYTHONHOME</code> and <code>PYTHONPATH</code> for cygwin gdb was able to run successfully(like in this answer: <a href="https://stackoverflow.com/a/19377110/2066736">https://stackoverflow.com/a/19377110/2066736</a>). So that's why in my <code>launch.json</code> I set the environment variables like so:</p>

<pre><code>"environment": [
    {"name":"PYTHONHOME", "value": "/usr/"},
    {"name":"PYTHONPATH", "value": "/usr/lib/python2.7"}
],
</code></pre>

<p>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?</p>