I’m working on a windows application in C++, and I’d like to use cout for debugging purposes.
It seems that the effects of cout are hidden, is there a configuration option that will allow a console window to open with my debug information?
I’m working on a windows application in C++, and I’d like to use cout
Share
You have a couple of reasonable options. One is to rename your entrypoint from
WinMaintomain, so it’ll be built as a console application. That can still use things likeRegisterClassandCreateWindowjust fine.The obvious alternative would be to use
OutputDebugStringinstead ofcout. The output from that will show up in a debugger, or any number of programs built specifically to display what it produces (basically mini-debuggers that just don’t provide other debugging capabilities).A third solution I’d consider less reasonable is to call
AllocConsoleto allocate a console, then write to it. If you don’t mind doing all your writing with Windows-native console functions (WriteFile,WriteConsoleOutput, etc.) that’s fine. Unfortunately, it won’t initialize the C++ streams to work with the console, so you’ll need to either open file streams pointing the console, or else find and use the (undocumented) function that initializes the streams library for the console. While this can be made to work, you end up doing extra work to write non-portable code, and little (if any) advantage to make up for it (and in this case, “non-portable” can include not even working with other versions of the same compiler, at least if you invoke the library’s initialization function).