Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem outputting a simple Hello World using C++ in VSCode #583

Open
NathanLabott opened this issue Mar 22, 2017 · 35 comments

Comments

@NathanLabott
Copy link

commented Mar 22, 2017

I'm trying to simply display the message "Hello World" to the output however whenever I run the build task, nothing is output, it states there is an active task running right now when I try to rerun it, and creates an object file called main.o. Any help would be great.

@seva0stapenko

This comment has been minimized.

Copy link

commented Mar 22, 2017

Yeah, that's the behavior I observe on OS X 10.2. As a workaround you can run your app in the terminal window, but debugging in combination with an occasional printf is currently does not seem possible.
Btw, this issue was presumably already reported - #513, at least it seems to be related.

@pieandcakes pieandcakes added debugger and removed debugger labels Mar 28, 2017

@pieandcakes pieandcakes self-assigned this Mar 28, 2017

@pieandcakes

This comment has been minimized.

Copy link
Collaborator

commented Mar 28, 2017

@NathanLabott Can you show me your task.json? The build task support is through VSCode itself and not part of this extension but I can take a look and see if I can see anything obvious.

@NathanLabott

This comment has been minimized.

Copy link
Author

commented Mar 29, 2017

screen shot 2017-03-28 at 9 51 31 pm

@pieandcakes

This comment has been minimized.

Copy link
Collaborator

commented Mar 29, 2017

@NathanLabott What OS are you using? If it is Linux, try g++ but also specify the -g option

@NathanLabott

This comment has been minimized.

Copy link
Author

commented Mar 29, 2017

screen shot 2017-03-29 at 3 29 28 pm

@pieandcakes

This comment has been minimized.

Copy link
Collaborator

commented Mar 29, 2017

Can you change the following?
"command": "g++"

"args": ["-g", "main.cpp", "-o", "<output_executable_name>"]
NOTE: Please replace <output_executable_name> with your executable name

and then put the executable name inside your "program" path for the launch.json.

@dmbelo

This comment has been minimized.

Copy link

commented Apr 9, 2017

I have the same issue whereby after setting up my tasks.json file my code compiles successfully but does not output any cout streams to the output console. I am running macOS 10.12.4 and am not trying to debug, simply spit messages to the output console.

My same experiment works on Windows by the way...

tasks.json for reference...

{
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": ["-g", "-o", "main", "main.cpp"],
    "showOutput": "always"
}
@pieandcakes

This comment has been minimized.

Copy link
Collaborator

commented Apr 10, 2017

@dmbelo Are you expecting messages during execution of your program? If so, are you debugging? If you aren't debugging and just running the application, please reach out to the VSCode Issues Page and get help. As stated above, we provide debugging support and not task.json support. If you are using the debugging feature , ensure that "externalConsole": true is set and you should see a pop-up terminal window with your output.

@dmbelo

This comment has been minimized.

Copy link

commented Apr 13, 2017

Thanks @pieandcakes my bad not realizing that you're not providing support for the task.json portion. I'm not encountering any issues during debugging. Cheers.

@jk21

This comment has been minimized.

Copy link

commented May 24, 2017

Hello,

i have the same problem! not showing anything in the output on osx.
When i set externalConsole to false i have the output in the debug console but cant handle stdin there...

my launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb",
            "preLaunchTask": "gcc"
        }
    ]
}

my task.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "gcc",
    "isShellCommand": true,
    "args": ["-g", "program.c", "-o", "program" ],
    "showOutput": "always"
}

my program.c

# include <stdio.h>

int main()
{
	printf ("Hello World!");
}
@saeedtabrizi

This comment has been minimized.

Copy link

commented Jul 20, 2017

Hi , I have same problem by vscode :
Version 1.14.1
Commit 2648980a697a4c8fb5777dcfb2ab110cec8a2f58
Date 2017-07-13T19:12:01.887Z
Shell 1.6.6
Renderer 56.0.2924.87
Node 7.4.0
Os : Linux ubuntu 17.04

Is there any fix or workaround here ?
Thanks

@pieandcakes

This comment has been minimized.

Copy link
Collaborator

commented Jul 20, 2017

@saeedtabrizi Please post your launch.json. The above reports are on mac OS (which I haven't been able to duplicate).

@pieandcakes

This comment has been minimized.

Copy link
Collaborator

commented Jul 20, 2017

@jk21 If this is still an issue, what I have found is that it looks like until you add a '\n' character at the end of your "Hello World" that the output doesn't get flushed to the terminal. That looks to be a problem with the program running under debugger (lldb). I think stdout gets redirected under debugger and it looks like they aren't flushing the buffer.

@saeedtabrizi

This comment has been minimized.

Copy link

commented Jul 20, 2017

Hi @pieandcakes ,

My project configuration is the following lines :

tasks.json :

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {   
            "taskName": "build",
            "command": "g++",
            "args": ["-g", "main.cpp"]
        }
    ]
}

launch.json :

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

Os details :
uname -a : Linux saeed-lenovo 4.10.0-26-generic #30-Ubuntu SMP Tue Jun 27 09:30:12 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

and my hello world in program is :

/// My C++ Program
#include "stdio.h"
#include "iostream"
using namespace std;

int main()
{
    int a = 4; // 000001000 => 4
    int b = 0;
    int c = 0;
    c = a << (b + 1); // 000010000 => 8
    c = a << (b + 2); // 000100000 => 16
    c = a << (b + 3); // 001000000 => 32
    c = a << (b + 4); // 010000000 => 64
    c = c >> (b + 1); // 001000000 => 32
    c = c >> (b + 1); // 000100000 => 16
    c = c >> (b + 1); // 000010000 => 8
    cout << "\n\nHello Wolrd!\n";
    cout << "\nMy name is saeed\n";
    std::cout << "I'm a C++ program\n";
    std::cout << c;
    printf("Hello");
    return 0;
}

When i run a.out via bash , output is normally print messages but when i launch via vscode i don't see any out put in opened terminal .

I can launch debugger normally and breakpoints working true but there is no any print message to output terminal .

Is this a vscode debug console problem or my configuration have any problem ?
I have upload my program as a zip file here .
HelloWorld.zip

Thanks .

@pieandcakes

This comment has been minimized.

Copy link
Collaborator

commented Jul 20, 2017

@saeedtabrizi A few things:

  1. Debugging will out put to an external window. On *nix we require gnome-terminal with a fallback to xTerm if gnome-terminal is not available.
  2. What happens is that when you start debugging, it will open a window and when debugging ends, this window closes. This is not the debugConsole in VSCode, but a separate terminal window that opens. Please check for that.
@saeedtabrizi

This comment has been minimized.

Copy link

commented Jul 20, 2017

@pieandcakes :

  1. I'm using gnome terminal exactly .
  2. when i start debugging the external console opened and at end of debugging the launched terminal closed normally , but the main problem is no printing any message that i write in my code .

@bobbrow bobbrow added the debugger label Aug 4, 2017

@mandemeskel

This comment has been minimized.

Copy link

commented Aug 6, 2017

Same issue for me, happened after I updated. I have a launch.json that is mostly identical to the ones posted above. Unfortunately, I don't remember which update caused this problem but it was working in May.

However, I created a work around using tasks.json:

     {
            "taskName": "build-debug",
            "command": "g++",
            "type": "shell",
            "args": [
                "-g","f9",
                "-Wall",
                "-o",
                "${fileBasenameNoExtension}-debug",
                "${relativeFile}",
                "-std=c++11",
                "-I",
                "/usr/include/c++/igloo-igloo.1.1.1"
            ],
            "problemMatcher": [
                "$gcc"
            ],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            }
        },
        {
            "taskName": "run-debug",
            "command": "gdb",
            "type": "shell",
            "args": [
                "${fileBasenameNoExtension}-debug"
            ],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "shared"
            }
        }

My keybindings in keybindings.json:

    {
        "key": "shift+f8",
        "command": "workbench.action.tasks.runTask",
        "args": "build-debug"
    },
    {
        "key": "shift+f10",
        "command": "workbench.action.tasks.runTask",
        "args": "run-debug"
    }
@Anandesh-Sharma

This comment has been minimized.

Copy link

commented Sep 10, 2017

Just create the two files under .vscode folder i.e. launch.json and tasks.json. And also you have MinGW installed on your machine. Copy and paste these line of codes in these two files.
1. Launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build cpp programs"
        }
    ]
}

NOTE: For miDebuggerPath add the path to the gcc debuger which typically can be found in bin folder.
2. Tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build cpp programs",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g", "${fileBasename}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

To run the code just press f5 functional key and that should work.

@jwinkler2083233

This comment has been minimized.

Copy link

commented Feb 2, 2018

I added a call to 'fflush(stdout);' before the program ended, and then the output appeared on the floating terminal window.

Ubuntu 14.04
VS Code
g++

@ripopov

This comment has been minimized.

Copy link

commented Mar 2, 2018

The problem is that terminal on linux closes immediately after debugged process exits, so you never know what was the output.

@mortinger91

This comment has been minimized.

Copy link

commented Mar 27, 2018

Same issue for me, not being able to debug a simple hello world c++ program, tried with many different "lanch.json" configuration but had no luck. The only solution i found for now is simply use this extension for everything but debugging and for that use "Native Debug" by "webfreak" choosing GDB. For what i can see it works pretty good

@ftrofin

This comment has been minimized.

Copy link

commented Apr 26, 2018

I think the default should be "externalConsole": false. The current behavior (on Mac) is to open a new terminal window very time you debug (which usually stays behind the VSC window so you don't even know it's there) and leave it open when the debugging session end. You can quickly accumulate a whole stack of those windows without even knowing they are there.

Setting it to true might make sense if you have a command line app that reads from stdin (maybe). In any case, a comment in the launch.json might be useful.

Also, I noticed that the documentation about debugging (https://code.visualstudio.com/docs/editor/debugging) does not say anything about this setting "externalConsole" but instead it mentions something else: "console - what kind of console to use, for example, internalConsole, integratedTerminal, externalTerminal." Maybe it's out of date?

Thanks and keep up the good work!

Cheers,

F.

@gurkanctn

This comment has been minimized.

Copy link

commented May 8, 2018

Same issue, of not seeing console outputs in the Debug window (Win10, gdb).

I'm able to debug the program through breakpoints, but I can't see the outputs. At the finalization of the debug session, I see the first line of my program's output, and the window disappears.

I'd appreciate a workaround, and also a solution to this issue. Flushing the output by "\n" or std::endl doesn't seem to work...

Till then, sadly, VSCode does not work with C++, at least for me! :(

@bobbrow

This comment has been minimized.

Copy link
Member

commented May 8, 2018

On Windows, I was able to redirect stdout to the Debug Console by adding

    "windows": {
        "externalConsole": false
    }

to launch.json

@sandakith

This comment has been minimized.

Copy link

commented Sep 18, 2018

In Ubuntu it would always use a basic type of console (that does not have any copy/paste features also). And externalConsole: false/true does not redirect to Debug console. is there a solution to copy content from the DebugerTerminal that VSCode use in Ubuntu/Linux?

@ismailhkose

This comment has been minimized.

Copy link

commented Nov 25, 2018

Here are my tasks.json and launch.json files. It doesn't show stdout from my simple hello world c++ example. I tested this on my MAC.

I am expecting to see stdout messages while debugging.

Any idea?

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "windows": {
                "command": "${workspaceRoot}/build.cmd",
                "args": [
                    "<Path/To/MinGW/Cygwin/Bin/Folder>",    // Path to the bin folder containing g++ to compile
                    "${fileBasenameNoExtension}.exe"                               // Output executable name
                ]
            },
            "linux": {
                "command": "g++",
                "args": [
                    "-g",
                    "${file}",
                    "-lpthread",
                    "--std=c++17",
                    "-o",
                    "${fileBasenameNoExtension}.out"
                ]
            },
            "osx": {
                "command": "g++",
                "args": [
                    "-g",
                    "${file}",
                    "-lpthread",
                    "--std=c++11",
                    "-o",
                    "${fileBasenameNoExtension}.out"
                ]
            }
        }
    ]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "preLaunchTask": "build",
            "type": "cppdbg",
            "request": "launch",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true,
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "logging": {
                "engineLogging": false,
                "trace": false
            },
            "windows": {
                "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",
                "MIMode": "gdb",
                "miDebuggerPath": "<Path/To/GDB>" // Path to gdb on windows
            },
            "linux": {
                "program": "${workspaceRoot}/${fileBasenameNoExtension}.out",
                "MIMode": "gdb"
            },
            "osx": {
                "program": "${workspaceRoot}/${fileBasenameNoExtension}.out",
                "MIMode": "lldb"
            }
        }
    ]
}

@bobbrow
@sean-mcmanus
@pieandcakes

@alexgomes09

This comment has been minimized.

Copy link

commented Apr 29, 2019

After 2 years, I still see this issue. Just trying to print a simple "Hello World!" but won't print anything within integrated terminal, however I see no error. Following is the snippet, would appreciate if someone shed some light on. I am on mac btw!

Update: Build task creates executable file fine. If I navigate and double click on main executable I see my cout message which opens in terminal app. I was hoping to see the output inside integrated terminal window as soon as I build.

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "helloWorld",
            "type": "shell",
            "args": [
                "-g",
                "-Wall",
                "-std=c++11",
                "${workspaceFolder}/main.cpp",
                "-o",
                "${workspaceFolder}/main"
            ],
            "command": "g++",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "panel": "shared",
                "showReuseMessage": false,
                "clear": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/main",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "helloWorld"
        }
    ]
}

main.cpp

#include <iostream>

using namespace std;

int main()
{
    int x = 10;
    int y = 20;
    cout << "Hello World!\n" << endl;
}
@mshibanami

This comment has been minimized.

Copy link

commented May 3, 2019

I ran @alexgomes09's code on Mac and it was output like this:

fff

I don't think the standard I/O is shown in the integrated terminal window (i.e. "TERMINAL" window), but the Debug Console window and I think it's intended behavior. But at first I actually misunderstood because when the debugging was started, the Debug Console window wasn't focused automatically. I wish it was...

By the way, as you can see from the above screen capture, the output is surrounded by @"..." like @"Hello World!\r\n" or @"\r\n". Is this intended? Can I trim this? Also, displayed newline \r\n.

@pieandcakes

This comment has been minimized.

Copy link
Collaborator

commented May 3, 2019

@mshibanami The Debug Console is showing the debugger output. The output you see there is what the Debugger is logging, which is that you have output to stdout. You need to set "externalConsole" : true in your launch.json or else lldb does not display anything. The integratedTerminal on macOS is not working due to a limitation of the version of lldb we use.

@alexgomes09

This comment has been minimized.

Copy link

commented May 3, 2019

@mshibanami @pieandcakes It actually worked the way I wanted. Had to add one more task which depends on other one. I realized, task that I posted was always compiling fine and then once I executed binary using external terminal.app it printed Hello World! . So here I wrote another task to execute the binary after compilation. Following is my final tasks.json

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile",
            "type": "shell",
            "args": [
                "-g",
                "-Wall",
                "-std=c++11",
                "${workspaceFolder}/main.cpp",
                "-o",
                "${workspaceFolder}/main"
            ],
            "command": "g++",
            "presentation": {
                "panel": "shared",
                "showReuseMessage": false,
                "clear": true
            },
        },
        {
            "label": "run",
            "type": "shell",
            "command": "${workspaceFolder}/main",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn": [
                "compile"
            ],
            "presentation": {
                "panel": "shared",
                "showReuseMessage": false,
                "clear": false
            },
            "problemMatcher": []
        }
    ]
}

PS: launch.json is untouched

Result :)

Screen Shot 2019-05-03 at 1 54 15 PM

As you see, it's showing within integrated terminal window. It even shows warning.
Modify code > hit Ctrl + Shift + B to run task and boom I have it. Just what I needed (for now)

However, I soon realized these won't keep me going for too long. Because it's just compiling one source code. Therefore I am after cmake build now.

@mshibanami

This comment has been minimized.

Copy link

commented May 4, 2019

@pieandcakes

The integratedTerminal on macOS is not working due to a limitation of the version of lldb we use.

Thanks, that's what I actually wanted to know. So @"..." also comes from the lldb. Thanks for clarifying.

@Sharrp

This comment has been minimized.

Copy link

commented Jun 10, 2019

@alexgomes09 that's clever, thank you!

@mattiasljungstrom

This comment has been minimized.

Copy link

commented Jul 31, 2019

It looks like the output in VS Code is buffered on Win10. This code will output into the debug console:

  setvbuf(stdout, NULL, _IONBF, 0);    // disable output buffer
  printf("Hello World\n");

@kushalmahajan

This comment has been minimized.

Copy link

commented Aug 4, 2019

@mshibanami The Debug Console is showing the debugger output. The output you see there is what the Debugger is logging, which is that you have output to stdout. You need to set "externalConsole" : true in your launch.json or else lldb does not display anything. The integratedTerminal on macOS is not working due to a limitation of the version of lldb we use.

Right. I came here searching for how to pipe output to external ZSH shell when I hit CTRL-SHIFT-B.

What do I need to do after setting externalConsole: true. Also, in VSCode Settings>Features>Terminal is set to external. Still there is no output.

Settings are as per docs - https://code.visualstudio.com/docs/cpp/config-clang-mac

What can I do to flush to external window?

@teohaneksi

This comment has been minimized.

Copy link

commented Sep 9, 2019

I also could not get any output, then i changed
"externalConsole": true -> "externalConsole": false in launch.json file.
This worked for me, though, i do not know why it worked exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.