我正在使用VS代码,cout无法正常工作

时间:2019-09-11 23:46:09

标签: c++ visual-studio-code cout

我在Ubuntu 19.04上使用VS代码,并且试图运行一个非常简单的程序:

int main(int argc, char *argv[]) {
    cout << "hello";
    //some other simple stuff

我正在使用gdb逐步执行此代码。我的launch.json文件如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/project1_debug",
            "args": ["-c queue", "-f stack"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]

cout下的另一个“简单的东西”可以正常工作,只是cout不起作用。我检查了输出,问题和调试控制台,它们都没有显示任何输出。

这是我的调试控制台所说的:

Loaded '/lib64/ld-linux-x86-64.so.2'. Symbols loaded.

Breakpoint 1, main (argc=3, argv=0x7fffffffdeb8) at project1.cpp:6
6   int main(int argc, char *argv[]) {
Loaded '/lib/x86_64-linux-gnu/libstdc++.so.6'. Symbols loaded.
Loaded '/lib/x86_64-linux-gnu/libgcc_s.so.1'. Symbols loaded.
Loaded '/lib/x86_64-linux-gnu/libc.so.6'. Symbols loaded.
Loaded '/lib/x86_64-linux-gnu/libm.so.6'. Symbols loaded.

Breakpoint 2, main (argc=3, argv=0x7fffffffdeb8) at project1.cpp:8
8       cout << "hello";
Execute debugger commands using "-exec <command>", for example "-exec info registers" will list registers in use (when GDB is the debugger)
Kill the program being debugged? (y or n) [answered Y; input not from terminal]

我不明白为什么cout不起作用。

3 个答案:

答案 0 :(得分:0)

我刚刚在externalConsole中将launch.json设置为true,现在可以在外部控制台(即新的bash)中获得所需的输出

答案 1 :(得分:0)

#include <iostream>,如果您不在此命名空间之外,请不要忘记使用std::cout

答案 2 :(得分:0)

输出在TERMINAL标签中,从下拉菜单中选择“数字:cppdbg:文件名”标签