What can explain std::cout not to display anything?
Asked Answered
D

5

16

For whatever reason, std::cout does not display anything with my application. The description of my development environment follows.

I am working on a Qt application using Qt Creator. Since Qt Creator can't be launched from my station (XP64), i am currently developping it with Visual Studio 2008 and the Qt plugin (by importing the .pro project file). Everything seems fine and the application works.

In some cases (depending on command line arguments), i don't want to launch the HIM, just to display a few sentences in the CLI (command line required arguments, for instance).

I don't get any error, but nothing is displayed. The corresponding code, which i am sure is run is the (classical) following :

std::cout << "is this going to be displayed ?" << std::endl;

Do you have any idea why nothing is displayed ?

Demote answered 13/4, 2010 at 8:5 Comment(2)
Why Don't you put a break point at this line and see if this piece of code is really executed or not...? is it executed...?Boyfriend
I had a similar issue, but it was because I wrote std::cout << "Number of SIFT Keypoints: " + siftImageKeypoints.size() << std::endl;. Resolution was to replaced the + with <<.Rappel
D
19

Ok, answer found. Simple answer, of course, as always when encountering such problems. Michael Aaron was on the right tracks.

Simply changing SubSystem to Console in project configuration (/Configuration properties/Linker/System) makes the whole thing work. The GUI still works, but with a background console. I can deal with that.

Demote answered 13/4, 2010 at 8:17 Comment(1)
There is still something wrong with that because I can easily see cout which I typically cannot see if I use cygwin's shell to launch the program. That's probably because most WinAPI programs use WriteConsole to output data instead of cout and stdout.Merrythought
G
27

On Windows, programs usually are built as either a SUBSYSTEM:WINDOWS application or as SUBSYSTEM:CONSOLE.

Programs built with SUBSYSTEM:CONSOLE are expected to be text-mode applications. For this type of application, stdout and stderr print to the console that you launched them from, creating a new console if necessary.

In contrast, SUBSYSTEM:WINDOWS applications do not bother with a console. You can still write to stdout and stderr, but they normally don't go anywhere. You could use AllocConsole to create a console to print to, but this will always print to a newly created console window, not to a console window you launched the program from.

One trick for SUBSYSTEM:WINDOWS applications is that even though there's no console, you can still pipe stdout and stderr. To pipe stdout, you can do:

YourApplication.exe > output.txt

or if you have cat (or an equivalent):

YourApplication.exe | cat

Also note that there's not really any difference between SUBSYSTEM:WINDOWS applications and SUBSYSTEM:CONSOLE applications other than how Windows treats them when creating the process. (You can create windows in SUBSYSTEM:CONSOLE applications.) You therefore can easily switch between SUBSYSTEM types (for example, to use SUBSYSTEM:CONSOLE for debug builds and SUBSYSTEM:WINDOWS for release ones).

Galloglass answered 13/4, 2010 at 8:27 Comment(1)
to get stderr too: YourApplication.exe 2>&1 | catUnsaddle
D
19

Ok, answer found. Simple answer, of course, as always when encountering such problems. Michael Aaron was on the right tracks.

Simply changing SubSystem to Console in project configuration (/Configuration properties/Linker/System) makes the whole thing work. The GUI still works, but with a background console. I can deal with that.

Demote answered 13/4, 2010 at 8:17 Comment(1)
There is still something wrong with that because I can easily see cout which I typically cannot see if I use cygwin's shell to launch the program. That's probably because most WinAPI programs use WriteConsole to output data instead of cout and stdout.Merrythought
D
9

Try

CONFIG += console

in your .pro file.

Declarant answered 13/4, 2010 at 23:8 Comment(1)
Don't forget to mark as answer, if you are satisfied with this reply and think that it solves your problem (which you obviously do).Higbee
I
4

Windows distinguishes between console applications and GUI applications, and does not create a console for GUI applications, by default (see this page from MSDN). You can use AllocConsole to create one.

Ilse answered 13/4, 2010 at 8:12 Comment(3)
@Benoit, it doesn't matter where you launch it... it depends entirely on the application, itself (whether is uses the WinMain or other functions associated with the Windows GUI).Ilse
It's not only the console that needs to be created, it is also the standard file handles for in, out and err that need to be openedKohlrabi
@MichaelAaronSafyan it DOES matter where you launch it from. I can easily see cout which I typically cannot see if I use cygwin's shell to launch the program but I cannot see it from CMD.Merrythought
F
0

Perhaps it's not the std::cout line that makes it not displayed, but the function containing it. Maybe it's not invoked at all, and that's why std::cout doesn't work.

Fusspot answered 13/4, 2010 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.