c++visual-studio-codevscode-debugger

How can I set up debugging C++ programs with command-line arguments in VS Code?


I am having a problem setting up my debugger in VSCode for C++. I wrote some code, then ran into some errors while running it so I decided to debug. I think thats when VSCode created the task.json file. Then I realized I wanted to use command line arguments and google/stackoverflow said to create a launch.json file which I did. It was empty so I pressed the add configuration button and got 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": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description":  "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }

    ]
}

Now I tried to fill the placeholders but I can't do them without getting tons of errors after errors. Could someone please post an example of a working launch.json and how the file paths are supposed to work?

My main goal is to debug my code while using command line arguments.


Solution

  • Here is a sample from a project I currently use:

    
        {
          "version": "0.2.0",
          "configurations": [
            {
              "name": "(gdb) Launch Test",
              "type": "cppdbg",
              "request": "launch",
              "program": "${workspaceRoot}/build_amd64/bin/tester_config",
              "args": [],
              "stopAtEntry": false,
              "cwd": "${workspaceRoot}/build_amd64",
              "environment": [],
              "externalConsole": false,
              "MIMode": "gdb",
              "setupCommands": []
            }
          ]
        }
    
    

    If you post your launch.json with the filled in values then I might be able to tell you exactly what's wrong, because assuming you filled out everything correctly, you should be working.