SDL Console output works when debugging but not when run from exe

I am writing an experimental networking program, basically a test program to train the network. I'm using SDL and SDL_net in Code :: Blocks with mingw, so the console output was directed to stdout.txt. I searched and found that you can fix this by including after SDL_Init ():

freopen("CON", "w", stdout); //stops redirect of output
freopen("CON", "w", stderr); //and errors...

      

This worked fine, but only when building and running the program in the IDE: when running outside of the IDE (like double clicking on the program), the program runs correctly, except for the console output, which is still empty, Since the program must be a console program, this is serious problem ... I don't want to always run the program in the IDE to use it.

Any solution is appreciated, but I would prefer it to be a code change, although a batch file will work in this case (I read a couple of posts where this is the only one that works, but they didn’t I can’t detail it, so I don’t can reproduce it). Thank.

+3


source to share


4 answers


Have you looked at the SDL Console FAQ ?

They provide many suggestions, including:

Try first

freopen( "CON", "w", stdout );
freopen( "CON", "w", stderr );

      

If that doesn't work (as in your case) try



#include <fstream>
#include <iostream>
using namespace std;
....
ofstream ctt("CON");
freopen( "CON", "w", stdout );
freopen( "CON", "w", stderr );
...
ctt.close();

      

OR

FILE * ctt = fopen("CON", "w" );
freopen( "CON", "w", stdout );
freopen( "CON", "w", stderr );
...
ctt.close();

      

Another option is to recompile the library, SDLmain

or add code SDLmain

to your project and stop linking to the library.

0


source


For (MinGW) SDL 2.0 Users

SDL 2.0 disables the console by default, or rather, it doesn't enable it. Since compiling with -mwindows

disables the console, stdout points to nothing. SDL_Log is a bit interesting and can find the console using the windows API, but it can't grab stdin because it cmd.exe

stole it :( .

SDL does not claim to be a console for various reasons, most likely because the program was compiled with -mwindows

. Specifying WinMain

as an entry point can also do a cmd.exe

console restore. From what I've read SDL_main

it is possible to redirect stdout and stderr.

Perhaps you can succeed with #undef

'main, and / or

  • call AllocConsole();

    afterSDL_init(...);

  • use freopen("CON", stdout)

  • compile without -mwindows

    ( #undef

    'ing main

    seems to have no effect)


but you should really just redirect stdout for example. cat, c main | cat

(Always follow the "Don't #undef

main if you don't need" rule ).

TL; DR

To record stdout

, simply redirect your program for MinGW on cat

: main | cat

. This is a hack that just makes sure stdout and stderr don't point to anything. To grab both stdin

and stdout

, compile without -mwindows to create a Windows console app. A new window will open if necessary.

Note. It is especially important to disable the output when using one of the above methods. Relying on line buffering is bad practice anyway.

+2


source


(I couldn't post this yesterday because I didn't have enough reputation)

Ok, experimented a little on the lines of the batch file My final and (almost) working batch file:

program.exe

      

Didn't understand it would be that easy, but still can't figure out why double-clicking on the program doesn't work. I said it almost works because after the client connected to the server, the console disconnected, so there is still a problem. As such, I will still greatly appreciate any help with this issue.

(End of yesterday's alleged post)

(Answer at the beginning of today)

I tried Emartel's suggestions but it still didn't work. Did some testing and found that the endless loop of printing a blank line was causing a blank screen issue after the client connected. Fixed the loop and now it works correctly, although I still need to use the batch script.

It would be helpful to know when anyone knows why double-clicking isn't working.

+1


source


Two reasons:

  • With SDL, the console is disabled. SDL uses windows, not consoles.

  • SDL redirects standard output (both cout and printf ()) to a file named stdout.txt in the same folder as the * .exe directory.

You can use:

std::cout << "Hello World";

      

And the message will be saved in stdout.txt file.

0


source







All Articles