6

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?

John
  • 7,114
  • 2
  • 37
  • 57
  • Is there a reason this was down-voted, because I don't see how this is a bad question. Just saying, I would appreciate feedback if it can be improved. – John Dec 31 '16 at 02:55
  • Looking back maybe I should have asked it on superuser? – John Jan 27 '17 at 03:22
  • Did You start vsc under windows on in cygwin prompt? I have similar issues but it behaves differently based on where it starts from. – MortenB May 10 '17 at 08:26
  • @MortenB I haven't looked at this in a while since I have recently been using my Mac. I believe I started it using git bash. That may have been the problem as I'm unsure whether git bash had g++. I could try taking a look at it again sometime soon. – John May 10 '17 at 15:04
  • @John, when you say "I have downloaded g++ through Cygwin and added it to my path" can you be more specific? What exactly did you add to your path? And where? Beginning? end? – SeeJayBee Feb 23 '19 at 03:55
  • @ChrisJ.Breisch Honestly I'm not sure what I meant exactly I asked this 2 years ago, but what I think I meant by "adding it to the path" is that the g++ executable was available on the Cygwin terminal. I don't think I had to do anything manually to do that I think just by installing the proper packages Cygwin did that automatically. Now that I look back at this I would probably just try this https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md again. I'm also primarily using a Mac, so I'd have to try this at home when I get a chance. If I get it working I'll post an answer. – John Mar 11 '19 at 22:09
  • By the way I don't think it was clear when I asked the question that I was using Microsoft's c/c++ tools for vs code which is here https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools. – John Mar 11 '19 at 22:11

0 Answers0