【发布时间】:2019-05-14 05:32:53
【问题描述】:
以这样的简单 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。
右侧标签:
如何解决这个问题?
设置:Ubuntu 18.04 上的 VS Code 1.33.1(官方 Snap 构建)
【问题讨论】:
-
可能通过在字符串中添加换行符。输出可能会被缓冲。
-
cout << "Hello World\n";甚至更好的cout << "Hello World" << endl;。endl输出换行符并刷新流。 -
谢谢,
endl工作了(\n没有),但 VSCode 的行为非常奇怪。 -
这不是 vs 代码行为,您的操作系统在打印到控制台之前会缓冲输出,这是完全正常的,并且在所有平台上都相当普遍
-
@john 最近似乎有关于使用
std::endl的相反信息,请参阅ACCU Overload 149 - Don't Usestd::endl。无论哪种方式,我都不认为这是一个炫耀,但对您的评论感到好奇。
标签: c++ visual-studio-code vscode-debugger