For (MinGW) SDL 2.0 users
SDL 2.0 disables the console by default, or rather, it does not enable it.
Since compiling with -mwindows
disables the console, stdout does not point to anything. SDL_Log is a little fancier and can find the console using the windows API, but it can't capture stdin, because cmd.exe
stole it :(.
SDL does not claim the console for various reasons, most likely because the program was compiled with -mwindows
. Specifying WinMain
as the entry point might also make cmd.exe
reclaim the console. From what I've read SDL_main
might redirect stdout and stderr.
You could probably get away with #undef
'ing main, and/or
- call
AllocConsole();
after SDL_init(...);
- use
freopen("CON", stdout)
- compile without
-mwindows
(#undef
'ing main
doesn't seem to have any effect)
but you should really just redirect stdout to e.g. cat, with main | cat
(Always follow the rule "Don't #undef
main unless you have to").
TL; DR
To capture stdout
for MinGW, simply redirect your program to cat
: main | cat
. This is a hack which simply makes sure stdout and stderr don't point to nothing.
To capture both stdin
and stdout
, compile without -mwindows to create a Windows console application. This will open a new window if necessary.
Note: It is especially important to flush output when using one of the above methods. Relying on line buffering is bad practice anyway.