VS Code C++ 程序在调试时不显示任何输出

VS Code C++ program does not display any output while debugging

拿一个像这样的简单 C++ 文件:

#include <iostream>
using namespace std;

int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cout << "Hello World";
    return 0;
}

return 0设置断点。设置此启动配置:

{
    "name": "g++ build and debug active file",
    "type": "cppdbg",
    "request": "launch",
    "program": "${fileDirname}/${fileBasenameNoExtension}",
    "args": [],
    "stopAtEntry": false,
    "cwd": "${workspaceFolder}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "setupCommands": [
        {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        }
    ],
    "preLaunchTask": "g++ build active file",
    "miDebuggerPath": "/usr/bin/gdb"
 }

转到左侧边栏中的调试选项卡并单击绿色 运行 按钮。

预期情况:我能在某处看到Hello World
实际情况:看不到到处Hello World

右侧标签:

如何解决这个问题?


设置:Ubuntu18.04

上的 VS Code 1.33.1(官方 Snap 版本)

根据上面的有用评论,我也会引用 Alan:

It's not vs codes behaviour, your operating system buffers output before printing to the console, this is entirely normal and fairly universal across all platforms

因此我需要在我的 std::cout 语句中添加一个额外的 std::endl