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
相关产品推荐
相关产品推荐

