在Visual Studio代码中调试C代码时,我的输出在哪里?
我有一个简单的c程序,在屏幕上打印一些东西。 当我调试程序时,我可以看到调试控制台,但是只要我使用fgets,我就看不到任何输出。使用VS代码进行调试时,我的程序运行在哪里 如果我只是运行编译的.exe,所有内容都会按预期打印在Visual Studio代码中调试C代码时,我的输出在哪里?,c,windows,visual-studio-code,vscode-debugger,C,Windows,Visual Studio Code,Vscode Debugger,我有一个简单的c程序,在屏幕上打印一些东西。 当我调试程序时,我可以看到调试控制台,但是只要我使用fgets,我就看不到任何输出。使用VS代码进行调试时,我的程序运行在哪里 如果我只是运行编译的.exe,所有内容都会按预期打印 #include <stdio.h> #include <stdlib.h> int main() { printf("Hello World!\n"); printf("Enter your name\n"); char name
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello World!\n");
printf("Enter your name\n");
char name[100];
// fgets(name, sizeof(name), stdin); // as soon as I uncomment this, no output is in the output console
printf("You name %s", name);
return 0;
}
一个解决方案可以解决你的问题 如何从VSCode配置
tasks.json
和launch.json
您不需要为此执行任何类型的编码,只需遵循以下步骤:
.vscode
文件夹中的tasks.json
和launch.json
F5
(调试快捷方式),关注该C程序文件,您将看到如下内容:GCC
(因为您试图调试一个C程序,并确保编译器已安装到您的系统中)launch.json
,如下所示:preLaunchTask
配置(位于配置的最底部)
F5
(这次是创建tasks.json
)后,您将看到如下所示,只需选择配置任务即可:tasks.json
,将标签
编辑为您在§4中选择的名称(请记住名称
)。换句话说,启动的preLaunchTask
和任务的标签应该相同。流程如下所示:
只需在launch.json中将
externalConsole:true
添加到您的配置中即可
例如:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "cl.exe build active file",
}
]
}
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
},
}
]
}
tasks.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "cl.exe build active file",
}
]
}
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
},
}
]
}
是否添加了断点在代码中?如果我添加一个断点,它会被命中,我可以逐步遍历代码,查看变量,但是标准的输入和输出不在调试窗格中,这让我们感觉到,因为它是只读窗口。请尝试重新配置
tasks.json
和launch.json
它们可能会修复您的错误。您可以发布整个tasks.json并启动吗。json?我没有使用gcc.exe,您提到的prelaunchtask根本不可见。那么您使用的是什么?@Liero you go。(zip仅包含任务和launch.json)。请不要忘记使用您自己的设置进行配置,否则如果只是复制粘贴而什么都不做,则可能无法使用。谢谢。但是,下次,请不要上载ZIP,而是在答案中包含所有相关详细信息。不要过度使用屏幕截图,因为无法从中复制文本。