SDL with MinGW, no more output to console

Hello,

I’m creating a project with SDL and I have realized that output does not
work in the console, instead everything is written to the output file
stdout.txt.

I have heard that creating GUI windows application causes any function
that write to stdout, stderr are redirected into the GUI application but
the problem is that my project is not a GUI application.

By the way I need to link against SDLmain if not it will complains about
lacks of WinMain at 16 which is my main problem. How do I prevent to link
agains SDLmain since I want a simple console application that uses SDL ?

This is my sample code and my CMakeLists.txt :

cmake_minimum_required(VERSION 2.8)
project(cmaketest C)

find_package(SDL REQUIRED)

include_directories(${SDL_INCLUDE_DIR})
add_executable(main main.c)
target_link_libraries(main ${SDL_LIBRARY} ${SDLMAIN_LIBRARY})

And the code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>

int
main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
puts(“HELLO”);
}

With this HELLO is not printed in the console but in the file stdout.txt

Cheers,–
David Demelier

Hi,

I don’t use CMake so I cannot help you with that part specifically.

If you were using Visual Studio, you would set the “subsystem” of your
project to console. You can continue to link to SDLmain, it provides a
console entry point too.

You can either re-redirect (or un-redirect) stdout and stderr, or
recompile SDLmain.lib with NO_STDIO_REDIRECT defined. See
http://forums.libsdl.org/viewtopic.php?t=7517 for a similar request
recently.

Alternatively, you can look at the Windows SDL_win32_main.c to see
what SDL is doing for you, and you can replicate as much (or all) of
that as you need in your own application. SDL_win32_main.c is in the
public domain, so you are free to copy/modify it without licensing
issues.

Regards,
– BrianOn 9 September 2011 12:38, David Demelier <demelier.david at gmail.com> wrote:

Hello,

I’m creating a project with SDL and I have realized that output does not
work in the console, instead everything is written to the output file
stdout.txt.

I have heard that creating GUI windows application causes any function that
write to stdout, stderr are redirected into the GUI application but the
problem is that my project is not a GUI application.

By the way I need to link against SDLmain if not it will complains about
lacks of WinMain at 16 which is my main problem. How do I prevent to link
agains SDLmain since I want a simple console application that uses SDL ?

add_executable(main main.c)

set_target_properties(main PROPERTIES WIN32_EXECUTABLE FALSE)

That should make it treat the thing as a console program instead of a
GUI app.

(and, I think, make it not need SDLmain linked in.)

Brian’s advice about NO_STDIO_REDIRECT is good, too.

–ryan.

Thanks the set_target_properties remove the need of SDLmain but the
stdout is still outside in a file, I will recompile SDL using
NO_STDIO_REDIRECT. Thanks

Cheers,On 09/09/2011 16:26, Ryan C. Gordon wrote:

add_executable(main main.c)

set_target_properties(main PROPERTIES WIN32_EXECUTABLE FALSE)

That should make it treat the thing as a console program instead of a
GUI app.

(and, I think, make it not need SDLmain linked in.)

Brian’s advice about NO_STDIO_REDIRECT is good, too.

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


David Demelier

Thanks the set_target_properties remove the need of SDLmain but the
stdout is still outside in a file, I will recompile SDL using
NO_STDIO_REDIRECT. Thanks

Maybe the attached patch is useful…so it won’t redirect stdio if we’ve
actually got a console to write it to. I’m not sure if I should apply
this, though.

–ryan.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: SDL-win32-no-redirect-stdio-on-console-RYAN1.diff
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110910/3cbb4b8e/attachment.asc

Hey.

It sounds like something people would certainly like. Lots of people
want to reconnect the console, certainly while developing their
program.

However, it could break people who currently rely on stdout.txt for
logging. It could be protected by a macro (not unlike SDL_NO_REDIRECT)
for the moment?

– BrianOn 11 September 2011 02:36, Ryan C. Gordon wrote:

Thanks the set_target_properties remove the need of SDLmain but the
stdout is still outside in a file, I will recompile SDL using
NO_STDIO_REDIRECT. Thanks

Maybe the attached patch is useful…so it won’t redirect stdio if we’ve
actually got a console to write it to. I’m not sure if I should apply this,
though.

–ryan.