[SDL] Quickfix to stdio redirection.

Started by
4 comments, last by Servant of the Lord 16 years, 1 month ago
In windows, the SDL library is compiled with the stdio redirection by default, which means that programs linked with it will have their standard output redirected to 'stdout.txt' and standard error to 'stderr.txt'. To prevent this from happening you would normally need to compile the source by yourself. Here is a way to avoid that. First, you have to make your project a win32 gui project (not console), in DevCpp that's in Project->'Project Options'. Then, you have to include 'windows.h' and 'stdio.h' in the file where you initialize SDL, and then your code should look like this: void initSDL() { SDL_Init(/*whatever*/); AllocConsole(); freopen("CONOUT$", "wb", stdout); freopen("CONOUT$", "wb", stderr); freopen("CONIN$", "rb", stdin); /*...*/ } Also, it's nice to call FreeConsole() when you're done. Hope this helped!
Advertisement
Wouldn't it just be easier to compile the application as a console application to begin with?
The difference between a "Console Application" and a GUI one, is that in the case of the console one, windows first allocates a console and directs output to it. That's great, but after it does that, SDL redirects it to the files, so it doesn't work. The reason I said it shouldn't be a Console App with this method, is so that you can allocate your own console. (it might work to keep it a console project, and just do the freopens, I haven't tried it because I'm not sure about the control a console application has over its console window).

EDIT: fixed a typo.
Actually, it doesn't. If you look at the SDL source code, specifically, SDL_win32_main.c, you'll see that SDL only redirects stdout and stderr to files if the program is built with the Windows subsystem. It doesn't do so when it's built in the console subsystem.
Well, if you try doing it in Dev-Cpp (making your project console), output won't be redirected to the console, I think redirection is on as long as sdlmain is compiled with --enable-stdio-redirect, or whatever compile-time option.
I'd also like to point out that this method works for me. I haven't tried your method, however, but it looks cleaner.

This topic is closed to new replies.

Advertisement