0

I am trying to pass command line arguments to the gdb in vscode. Here is my launch.js config.

{
"configurations": [
{
    "name": "(gdb) Launch",
    "type": "cppdbg",
    "request": "launch",
    "program": "${workspaceFolder}/main",
    "args": ["1"],
    "stopAtEntry": false,
    "cwd": "${fileDirname}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "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
        }
    ]
}
]

}

Here is the file and folder structure.

Folder structure for dummy project

Here is the simple sanity check code

#include<stdio.h>


int main(int argc, char* argv[])
{
   printf("argc == %d\n", argc);
   return 0;
}

Its output is

argc == 1

I am not able to capture argument. Please help.

1
  • My apology, I have added that. As for output, I was expecting argc val to be number of args I pass, that is argc value should be 2 instead of 1. Commented Jun 30, 2023 at 7:20

2 Answers 2

0

It appears that when the click on the button of at the upper right corner provided in vscode, it does not run as per config provided in launch.json config. To run it, go the right hand toolbar and click on debug button. Then make choose option of (gdb) lauch.json and run it. Then your code will run as per launch.json.

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

2 Comments

This might be one or the rare situation where screenshots might be useful. It could make it more clear what buttons are involved.
are you using the Code Runner extension?
0

The simplest way to pass arguments to gdb in vscode/vscodium is to use the Native Debug extension with a launch configuration like this

    {
        "type": "gdb",
        "request": "launch",
        "name": "Debug with gdb",
        "target": "./programname",
        "arguments": "some_program_argument",
        "cwd": "${workspaceFolder}/bin",
        "valuesFormatting": "parseText"
    }

Just for comparison, the corresponding configuration for lldb would be

    {
        "type": "lldb",
        "request": "launch",
        "name": "Debug with lldb)",
        "program": "./programname",
        "args": ["some_program_argument"],
        "cwd": "${workspaceFolder}/bin"
    }

For some reason the syntax is different for gdb compared to lldb, the first requires target/arguments the latter requires program/args.

1 Comment

After some vscode update, with "type": "gdb" doesn't seem to exist anymore.

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.