skip to Main Content

This is my simple program

#include <stdio.h>
int main(int argc, char *argv[]){ 
    printf("%dn", argc); 
    for(int i=1; i<argc; i++) printf("%sn", argv[i]); 
    return 0; 
}

If I compile with terminal and run

gcc -o test test.c
./test foo bar

I get

3
foo
bar

so works perfectly. Now I would like debug this program passing arguments <"foo"> <"bar"> but i don’t understand how.

I tried with CTRL + SHIFT + P -> "C/C++: edit Configurations (JSON)" and adding the line

"compilerArgs": ["bar", "foo"]

but still doesn’t work. Debugger says "argc: 1" when it should be 3
enter image description here

This is my launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name":"gcc - Build and debug active file",
            "type":"cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/Assignment1/",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "MIMode": "gdb",
            "externalConsole": false
        }
    ]
}

task.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        },
    ],
    "version": "2.0.0"
}

What I’m doing wrong?

2

Answers


  1. Chosen as BEST ANSWER

    I solved, I missed to add "/test" in lauch.json:

    ...
    "program": "${workspaceRoot}/Assignment1/test"
    "args": ["bar", "foo"], 
    

    now everything works


  2. put them right

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name":"gcc - Build and debug active file",
                "type":"cppdbg",
                "request": "launch",
                "program": "${workspaceRoot}/Assignment1/",
                "args": ["bar", "foo"], // <<<<<===== here
                "stopAtEntry": true,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "MIMode": "gdb",
                "externalConsole": false
            }
        ]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search