You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code调试器中传递C程序命令行参数

解决VS Code调试C程序无法传递命令行参数的问题

问题根源

你的launch.json中args字段存在语法错误:数组元素之间缺少逗号分隔,导致调试器无法正确解析参数。另外,如果编译程序时未添加调试符号,也会引发调试无响应的情况。

修复步骤

  1. 修正args配置
    数组内每个命令行参数需作为独立字符串,且用逗号分隔。对应你需要的./a.out "Hello" "How are you",正确的args配置应为:

    "args": ["Hello", "How are you"]
    
  2. 编译时添加调试符号
    编译C程序必须加上-g参数,生成包含调试信息的可执行文件,否则gdb无法进行分步调试:

    gcc -g your_program.c -o a.out
    

修改后的完整launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.out",
            "args": ["Hello", "How are you"],
            "stopAtEntry": true, // 建议设为true,便于在程序入口处触发断点
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "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
                }
            ]
        }
    ]
}

额外检查项

  • 确保VS Code的C/C++扩展为最新版本,必要时重启软件重试
  • 确认program路径指向的是刚用-g编译生成的a.out,避免路径错误导致调试器找不到程序

内容的提问来源于stack exchange,提问作者pedrooc16

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 21:30:30