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

VSCode内置控制台调试C/C++程序失效问题求助

Windows下VSCode集成控制台调试C/C++无输出的解决方法

调整launch.json配置

把avoidWindowsConsoleRedirection设为false,同时保留externalConsole: false,再添加internalConsoleOptions: "openOnSessionStart"确保调试时集成控制台自动打开。修改后的完整配置如下:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Test123",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "avoidWindowsConsoleRedirection": false,
      "internalConsoleOptions": "openOnSessionStart",
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\mingw\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "为gdb启用自动结构化和缩进",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "C/C++: gcc.exe build active file"
    }
  ]
}

修改编译选项关闭输出缓冲

打开tasks.json(通过VSCode左侧“运行和调试”面板的“创建tasks.json文件”或“配置任务”入口找到),在gcc的编译参数里添加-fflush-all-gtocs强制刷新输出缓冲,避免输出被缓存导致看不到内容:

{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: gcc.exe build active file",
      "command": "C:\\mingw\\bin\\gcc.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "-fflush-all-gtocs"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "编译器: C:\\mingw\\bin\\gcc.exe"
    }
  ],
  "version": "2.0.0"
}

代码层面临时处理(可选)

如果不想改编译配置,也可以在代码里每次输出后手动刷新缓冲区,比如:

printf("输出内容\n");
fflush(stdout); // 强制把缓存的输出打印到控制台

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 21:17:26