1

Over the weekend I had installed a gcc through the MSYS2 bash. I set it up in VS code and have it working properly. I even had the GDB working (yes I know this is a debugger). But, my main question is, is it possible to use the debugging function in VS code to debug rather then GDB. Pressing F5 it pulls up the launch.json file and gives me launch: program 'enter program name, for example c:\School\a.exe' does not exist .After some research I see you give it a file to the args to allow it run in debugger. When I do this though I can't seem to either give it the right file or make it work overall. I am also using a.exe rather than a.out. I'm unsure if this has effect.

{
    // 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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": ["C:\\School\\CSE340\\project2\\main.cpp"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

1 Answer 1

3

VS Code does not have an internal debugger (see here). You need to use GDB or the visual studio debugger (if you have the latter).

In your launch.json you need to modify the entries:

"program": this is the path to the program you want to debug, i.e. your compiled program (can be a relative path to your project folder)

"miDebuggerPath": this is the path to GDB

"args": these are arguments, you want to pass to your programm for debugging purposes, i.e. you can leave this blank

So the launch.json file for you would look something like this:

{
    // 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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\CSE340\\project2\\main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", // Path where your gdb.exe is located
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

${workspaceFolder} is a path variable to your workspace and seems to point to C:\\School\\, so maybe you'll need to modify the value of "program" to point to the application that you want to debug. You could also specify the absolute path to your program.

Also, do not forget to compile your code with debug-flags (-g), these are needed by GDB in order to step through the code. For example: g++ -g main.cpp -o main.exe

Sign up to request clarification or add additional context in comments.

6 Comments

I'm not entirely sure this is true. I know for certain code supports step by step debugging for cpp. My issue is I am trying to do it using GDB. I'm just unsure of how to give it my file to begin debugging properly.
@AustinEfnor where did you read about the debugging without gdb? I can't find anything about it on the internet. Also this site says that VS Code only uses debug adatpers. Btw: why don't you want to use gdb?
I remember seeing someone do it somewhere. They gave it essentially what I did but they managed to pass in the GDB into the VS code debugger allowing them to step through like you can in Visual Studio (step over, step into, ect) all within VS Code. This is what I want to attain because it is what I prefer and how I wish my workflow to be. Also, using it in this sense allows me to see the variable in the debugger tab on the right and see what stack is being called, also ect. Just can't figure it out :(
@AustinEfnor Then we have been talking past each other: That's exactly what the launch.json file is for. VS Code invokes GDB (or any other debugger, that uses the same protocoll) and acts as a GUI for the debugger. I edited my answer for more informations.
Awesome! That got me to the point I wanted, however when I debug and I try to "step into", or any step, it gives Unable to open 'crtexe.c': File not found (file:///c:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c). I also changed "stopAtEntry" to true in order to get the GUI to show up but this is when it breaks while stepping. Also, btw it gives me an option to create this file in VS code, I am just worried this will cause more problems.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.