Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print to the console while an SDL 2 program is running?

I'd like to print some debugging stuff to the console while running my SDL 2 program, but it seems impossible. printf("Hi!\n") and SDL_Log("Hi!\n") both won't do me any good.

I even tried printing before initializing SDL (and after quitting it, too), but to no avail. It seems like merely importing the SDL library makes it impossible to print anything to the console.

Here are the parameters I'm compiling with, since it's possible that might have something to do with it:

g++ hello.cc -IC:\mingw_dev_lib\include\SDL2 -LC:\mingw_dev_lib\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -std=c++11

Any ideas?

like image 685
Coffee Maker Avatar asked Dec 31 '15 00:12

Coffee Maker


2 Answers

So, I figured out what's preventing me from seeing output. These compile options

-Wl,-subsystem,windows

essentially disable the console window, preventing output from being displayed. This is good for when a game is finished, but terrible for debugging. So, I went ahead and removed those compile options and now printf() and SDL_Log() work perfectly fine.

like image 100
Coffee Maker Avatar answered Sep 23 '22 06:09

Coffee Maker


Since this is still on going issue with SDL2 in window when using mingw, here is the better solution that I found and tested as working.

Do not remove the -mwindows build option as others have suggested. You should be adding `pkg-config --libs SDL2` as your build options but for the debug build options you should also add -mconsole at the end. It should come after the -mwindows flag.

Debug: `pkg-config --libs SDL2` -mconsole
Release: `pkg-config --libs SDL2`

Note: I'm compiling for Windows 10, SDL2 v2.0.9, Msys64, mingw64, Code::Blocks 17.12
`pkg-config --libs SDL2` expands to:
-LC:/msys64/mingw64/lib -lmingw32 -lSDL2main -lSDL2 -mwindows

References:
SDL2: keep the -mwindows flag in the pkg-config --libs output #2419
configure: force -mconsole when linking SDL under MinGW

like image 26
Amnesia Avatar answered Sep 26 '22 06:09

Amnesia