如何在VS Code调试器中传递C程序命令行参数
解决VS Code调试C程序无法传递命令行参数的问题
问题根源
你的launch.json中args字段存在语法错误:数组元素之间缺少逗号分隔,导致调试器无法正确解析参数。另外,如果编译程序时未添加调试符号,也会引发调试无响应的情况。
修复步骤
修正
args配置
数组内每个命令行参数需作为独立字符串,且用逗号分隔。对应你需要的./a.out "Hello" "How are you",正确的args配置应为:"args": ["Hello", "How are you"]编译时添加调试符号
编译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
相关产品推荐
相关产品推荐

