I have a .exe file that I’ve compiled on windows. But when I run it from the command line I don’t see any of the things my program outputs to std::cout or std::cerr. The program runs and continues to run even after the command line returns to the prompt (it’s a GUI program and doesn’t quit until I press the quit button). How can I see the output of my program?
I’m using cmake to make a visual studio project which I then compile with msbuild.
The simplest approach is to rebuild the program as a console application. The option to
link.exeneeds to be/SUBSYSTEM:CONSOLEinstead of/SUBSYSTEM:WINDOWS; presumably there is a straightforward way of specifying this in cmake.This change shouldn’t affect your GUI at all, but it will cause Windows to allocate a console if the process isn’t already associated with one. Also, command line shells will usually wait for console applications to exit before continuing.
The other approach is to call
AllocConsoleto explicitly create a new console, orAttachConsoleif you want to use an existing one. Or, of course, you could send the output to a log file.Additional
According to a Google search, you can build the program as a console application by adding the following line to your source code:
This is probably the easiest solution. You can put it in an
#ifblock if you only want the console for debug builds.See also CMake: How to use different ADD_EXECUTABLE for debug build?