Sign in to follow this  

[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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this