因此,我正在编写一个示例程序来学习如何在Visual Studio Code中构建和调试C项目。作为参考,这是我的项目的launch.json
和tasks.json
:
launch.json:
"version": "0.2.0",
"configurations": [
{
"name": "test-project",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\test.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build test-project"
}
]
tasks.json:
"version": "2.0.0",
"tasks": [
{
"label": "build test-project",
"type": "shell",
"command": "gcc",
"args": [
"-std=c99",
"-g",
"${workspaceFolder}\\test.c",
"-o",
"${workspaceRoot}\\test.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
这里是'test.c':
test.c
#include <stdio.h>
int main(int argc, char* argv[]){
int t = 9;
int p = 10;
return 0;
}
即使我未提供任何命令行参数,这也是我看到的命令行参数:
事实证明,您可以在监视面板中看到3个额外的命令行参数"2>CON"
,"1>CON"
和"<CON"
。即使我给了0个命令行参数,为什么会发生这种情况?
[在Visual Studio Code中进行了一些搜索和测试之后,我在"externalConsole"
中发现了一个名为launch.json
的选项,该选项将输出重定向到实际的命令提示符窗口,从而从命令行中删除了其他重定向。还有一个称为"avoidWindowsConsoleRedirection"
的选项,可以避免在不显示外部命令提示符的情况下添加额外的命令行参数/重定向。如果launch.json
文件中同时存在两个选项,则"externalConsole"
优先。下面同时使用的示例:
"version": "0.2.0",
"configurations": [
{
"name": "test-project",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\test.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"avoidWindowsConsoleRedirection": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build test-project"
}
]