2

Today I'm trying to switch from VS2019 to VS Code while keep working with MSVC. This way I will develop in a lightweight and easier environment most of the time, and when I need advanced stuff such as seeing hot paths, I would be able to open VS2019 and do the job. Below is my launch.json configuration

"name": "debug",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/build/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"preLaunchTask": "compile"

The problem is that when debugging the output from std::cout cannot be seen in any window. Setting externalConsole to true opens a new console where the results are the expected ones. Is there a way to see the output of my program without a new console? When I worked with Node some time ago I remember that console.log() was visible in the DEBUG CONSOLE window

Edit 1

I added screenshots of the bottom 4 tabs below for clarifying. As u can see DEBUG CONSOLE yeild nothing. The last image is the external console. I found that after ~ 60 secs the DEBUG CONSOLE window displays the right thing, and then I guess freezes again for another ~ 60 secs. The program should display lines in an infinite loop. The last screenshot is took in another debug session, after modifying launch.json

enter image description here enter image description here enter image description here enter image description here enter image description here

Edit 2

Below are 2 examples. The first one works as intended. The second one does not. In my initial program something similar to the second example was executed

#include <iostream>
using namespace std;
int main() {
    std::cout << "Hello world!\n";
}
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
    while(true) {
        Sleep(500);
        std::cout << "Hello world!\n";
    }
}

2 Answers 2

2

You are probably looking at the wrong tab. There should be the tabs "Problems", "Output", "Debug Console" and "Terminal". Why you hit F5 you are in "Terminal". That's the shell where your launch task get's executed. Your program output from std::cout however goes into "Debug Output".

Sign up to request clarification or add additional context in comments.

6 Comments

I edited my question regard ur suggestions. Please take a look again under Edit title
@uIM7AI9S Your launch.json is fine. Mine is identical to yours and i get all console outputs. The problem seems to be with your program. Did you try console output with a simple Hello World? It seems like your program just runs very slow.
I made another edit which contains 2 examples. One that works and one that doesn't
I managed to reproduce your problem. I am not exactly sure why but the problem is that youre using '\n'. If you use std::cout << "Hello world!" << std::endl; everything should work as you want. The difference between \n and std::endl is that \n doesn't flush the stream. I honestly don't know enough about output streams to tell what exactly is going on there or if this is some sort of bug in VSCode or not. I would have expected \n to work aswell to be honest. Maybe this is worth opening a separate question.
Seems like the stream must be flushed. I'm quite sure this is a bug since u can't write stuff inline over a longer period of time. Example: for(auto i = 0; i < 3; i++) {Sleep(500); std::cout << i;}. When using std::flush it is still not working. I will make another question and post link here
|
0

with debug intepreter, normally debugger will not flush the stream with "\n". you can flush the stream with std::endl or manually flush it with std::cout.flush() but this flush also need a line break char - \n. that should be:

std::cout << "Hello, Jesse!\n";
std::cout.flush();

or

std::cout << "Hello, Jesse!" << std::endl;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.