Search code examples
c++csdlsdl-2sdl-ttf

SDL_Init_Everything fails in C


I am trying to create a game in C using SDL. After some time of debugging I found an error at SDL_Init:

#include <SDL2/SDL_ttf.h>

int main(void) {
  printf("Init attempt\n");
  if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
    fprintf(stderr, "Failed at SDL init: %s\n", SDL_GetError());
    fflush(stderr);
    fflush(stdout);
    exit(EXIT_FAILURE);
  }
  printf("Init success\n");
  return EXIT_SUCCESS;
}

The output is the following:

Init attempt

Somehow the fprintf inside the write function is getting executed.

The gcc command I used is: gcc -Wextra -Wall -pedantic program.c -lpthread -lSDL2 -lSDL2_ttf

Note: If I use g++, everything works fine. However, I must use gcc.

Is there a way to fix the error?


Solution

  • using the official doc (https://wiki.libsdl.org/SDL_Init) there's a flag that might conflict with your setup: SDL_INIT_HAPTIC

    i didn't get any error or crash so im not sure, but it is possible that this flag might be included in the EVERYTHING one, and if so:

    It’s disabled by default if you don’t use ./configure; make style builds.
    Otherwise it’ll happily build if your headers are new enough.
    

    (https://discourse.libsdl.org/t/sdl2-sdl-init-sdl-init-everything-failing/19250)

    did you try setting up by flags you only need, like SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_AUDIO and see if this works?