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

sdl log
sdl windows

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?


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.

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  int threadFunction( void* data ) { //Print incoming data printf( "Running thread with value = %d ", (int)data ); return 0; } Our thread function is fairly simple. All it does is take in the data as an integer and uses it to print a message to the console.


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

4.1.3. Drawing to a screen using SDL2, What one can do in this case is write an application that can be run on a desktop also possible to set defines either in source code or in the compiler command line. Although SDL2 is a C library (and hence can be used from C++ directly),  I want to use an SDL-based program to display graphics on the console, without having to log on from the console, and without running the program as root. For example, I want to be able to run it via ssh. The target OS is raspbian. Here is a short example in python to illustrate the problem:


I use this approach for debugging console:

static ULONG_PTR GetParentProcessId() // By Napalm @ NetCore2K
{
    ULONG_PTR pbi[6];
    ULONG ulSize = 0;
    LONG (WINAPI *NtQueryInformationProcess)(HANDLE ProcessHandle, ULONG ProcessInformationClass,
            PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength); 
    *(FARPROC *)&NtQueryInformationProcess = 
        GetProcAddress(LoadLibraryA("NTDLL.DLL"), "NtQueryInformationProcess");
    if(NtQueryInformationProcess){
        if(NtQueryInformationProcess(GetCurrentProcess(), 0,
                    &pbi, sizeof(pbi), &ulSize) >= 0 && ulSize == sizeof(pbi))
            return pbi[5];
    }
    return (ULONG_PTR)-1;
}

static void _windows_init_console(int argc, char **argv) {
    (void)argc, (void)argv;
    ULONG_PTR ppid = GetParentProcessId();
    if(ppid == (ULONG_PTR)-1) {
        AllocConsole();
    } else {
        AttachConsole(ppid);
    }

    freopen("CONIN$", "r", stdin); 
    freopen("CONOUT$", "w", stdout); 
    freopen("CONOUT$", "w", stderr); 
}

(GetParentProcessId comes from How can a Win32 process get the pid of its parent?). Works quite fine, but I still cannot make it work with grep/findstr etc. (i.e. 'true' stdout with pipe redirection).

Sdl scancode mouse, Contribute to veandco/go-sdl2 development by creating an account on can add and remove cells with the mouse and some basic stats are printed to console. In the program, I am able to "Intercept" the functions of Leftclick, Rightclick, ect, at AdvanceMAME when using the sdl video driver, which is the default when run​  At the top of our source file we include SDL since we're going to need the SDL functions and datatypes to make any SDL code. We're also going to include C standard IO to print errors to the console. You're probably more used to using iostream, but I use printf in my applications because it's more thread safe.


Since SDL2 use the windows subsystem, you can open the console window by using Win32 API.

According to this blog post:

#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#include <SDL2/SDL.h>

int main(int argc, char *argv[])
{
    // SDL2 init code...

    // Just before the event loop
    AllocConsole();

    HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
    int hCrt = _open_osfhandle((long) handle_out, _O_TEXT);
    FILE* hf_out = _fdopen(hCrt, "w");
    setvbuf(hf_out, NULL, _IONBF, 1);
    *stdout = *hf_out;

    HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
    hCrt = _open_osfhandle((long) handle_in, _O_TEXT);
    FILE* hf_in = _fdopen(hCrt, "r");
    setvbuf(hf_in, NULL, _IONBF, 128);
    *stdin = *hf_in;

    // use the console just like a normal one - printf(), getchar(), ...
}

Hope this helps.

Sdl2 gui tutorial, If set, the SDL2 libraries and headers from this path are used when compiling kivy X RaspberryPi Kivy first run To write your first program you have to install Kivy. Re: Start SDL2 programs from console (emulationstation for Arcade Cabinet)  Now that's we've got the right libraries, we can put together a basic SDL program to test everything's working. It won't do anything yet, but it should at least compile and run without errors.


Printing to stdout/stderr fails with SDL import on Windows · Issue #86 , When importing SDL the following program will no longer print Also when I add "sdl2" to my build-depends in cabal it stops console output as well. sdl2-ttf causes breakage in a working program haskell-game/sdl2-ttf#13. The program, then enters the body of do..while loop without checking any condition (as opposed to while loop). Inside the body, product is calculated and printed on the screen. The value of i is then incremented to 2.


Using SDL2: Opening a Window, g++ -o sdl2-program main.cpp `sdl2-config --cflags --libs. clang++ -o Compile and run it. If so, print to the console letting the user know. We can use a while loop to continuously check if an event needs to be processed. An SDL 1.2 codebase is given with this tutorial, called QuickCG. All the C++ code in the articles uses QuickCG and its functions to draw graphics on the screen. This page will explain how to use QuickCG to get the examples working. Information is provided to get SDL and QuickCG working in the DevC++ IDE, as well as how to do ith with g++.


Opening an SDL Window: Handmade Penguin Chapter 2, Fortunately, initialising SDL is easy: we just use the SDL_Init function. We also We need to keep the program running while the window is on the screen, and quit when the user closes the window. We'll do Let's start by writing a function to handle an event. If we resize it, we can see the resize events in the console. This is a problem I have been having with every program I write since I started using SDL 2. Whenever I compile my code and run my program, everything works perfectly fine until at some point (usually after 3-8 minutes of running), the program will stop responding completely and I will have to exit out of the console to close it.