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

Time:10-13

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.

CodePudding user response:

You can find tutorials how to set up Visual Studio Code for C here:

https://code.visualstudio.com/docs/cpp/config-msvc

https://code.visualstudio.com/docs/cpp/config-mingw

A valid config should look something like this:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "C/C  : cl.exe build active file",
      "command": "cl.exe",
      "args": [
        "/Zi",
        "/EHsc",
        "/Fe:",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "${file}"
      ],
      "problemMatcher": ["$msCompile"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ]
}

As you can see, it's best to let the available extensions handle the setup, as getting all the compiler switches right can be tricky.

The full reference for tasks.json is found here: https://code.visualstudio.com/docs/editor/variables-reference

CodePudding user response:

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.

  • Related