【问题标题】:Where is my output when debugging C code in Visual Studio Code?在 Visual Studio Code 中调试 C 代码时我的输出在哪里?
【发布时间】:2020-05-28 15:24:04
【问题描述】:

我有一个简单的 c 程序,可以在屏幕上打印一些内容。 当我调试程序时,我可以看到 DEBUG CONSOLE,但是一旦我使用 fgets,我就看不到任何输出。使用 VS Code 调试时我的程序在哪里运行?

如果我只是运行我编译的 .exe,一切都会按预期打印。

#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;
}

我的launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(Windows) Launch",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${workspaceFolder}/app.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false
    }
  ]
}

【问题讨论】:

  • 你是否在代码中添加了任何断点?
  • 如果我添加一个断点,它会被命中,我可以通过代码,查看变量,但是标准输入和输出不在调试窗格中,这很有意义,因为它是只读窗口
  • 尝试重新配置tasks.jsonlaunch.json 他们可能会修复您的错误。

标签: c windows visual-studio-code vscode-debugger


【解决方案1】:

一个解决方案可能会解决您的问题。

如何在 VSCode 中配置 tasks.jsonlaunch.json

您无需为此进行任何类型的编码,只需按照以下步骤操作即可:

  1. 删除位于.vscode文件夹中的tasks.jsonlaunch.json
  2. 再次按F5(调试快捷方式)关注该C 程序文件,您会看到如下内容:

  1. 选择您的编译器 GCC(因为您正在尝试调试 C 程序并确保编译器已安装到您的系统中)。
  2. 系统会提示您选择配置(假设选择选项:GCC),您将获得由 VSCode 自动创建的launch.json,如下所示:

注意:记住preLaunchTask 配置(位于配置的最底部)。

  1. 再次按下F5(这次是创建tasks.json)后,您将看到如下所示的内容,只需选择配置任务

  1. 现在,您将被重定向到tasks.json,将label 编辑为您在第4 节中选择的名称(记住name)。也就是说,launch 的preLaunchTask 和tasks 的label 应该是一样的。流程如下图:

现在,您可以进行成功的调试了。一个工作示例:

【讨论】:

  • 你能发布整个tasks.json和launch.json吗?我没有使用 gcc.exe,您提到了根本不可见的 prelaunchtask。
  • 那你用什么?
  • @Liero there 你去吧。 (压缩包只包含任务和launch.json)。不要忘记使用您自己的设置进行配置,否则如果只是复制粘贴而没有任何内容,则可能无法正常工作。
  • 谢谢。但是,下次请不要上传 ZIP,而是在答案中包含所有相关详细信息。不要过度使用屏幕截图,因为无法从中复制文本。
【解决方案2】:

只需将externalConsole": true 添加到您在launch.json 中的配置中。

例如:

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",
    }
  ]
}

tasks.json

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

【讨论】:

    猜你喜欢
    • 2021-03-05
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 2012-04-10
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    相关资源
    最近更新 更多