[SDL] Quickfix to stdio redirection.

This topic is 3570 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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!

Share on other sites
Wouldn't it just be easier to compile the application as a console application to begin with?

Share on other sites
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.

Share on other sites
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.

Share on other sites
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.

Share on other sites
I'd also like to point out that this method works for me. I haven't tried your method, however, but it looks cleaner.

Share on other sites

This topic is 3570 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628710
• Total Posts
2984320

• 23
• 11
• 9
• 13
• 14