gcc Visual Studio代码-在C中调试时设置参数

xurqigkl  于 2023-03-18  发布在  其他
关注(0)|答案(2)|浏览(129)

这是我的简单程序

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

如果我用terminal编译并运行

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

我得到

3
foo
bar

现在我想调试这个传递参数〈“foo”〉〈“bar”〉的程序,但是我不明白怎么做。
我尝试使用CTRL + SHIFT + P -〉“C/C++:编辑配置(JSON)”并添加行

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

但仍然不起作用。调试器显示“argc:1”,而实际应为3 enter image description here
这是我的发布会

{
    "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"
}

我做错了什么?

oaxa6hgo

oaxa6hgo1#

纠正错误

{
    "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
        }
    ]
}
ljo96ir5

ljo96ir52#

我解决了,我错过了在lauch.json中添加“/test”:

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

现在一切正常

相关问题