SDL 2.0 won't initialize. Error: “Failed to connect to the Mir Server”

前端 未结 3 720
春和景丽
春和景丽 2021-01-20 04:17

I am running Ubuntu 14.04, and using Eclipse CDT.

In my program, I am trying to initialize SDL and if it doesn\'t initialize then output the error, but SDL_GetError(

相关标签:
3条回答
  • 2021-01-20 04:43

    bash$ export DISPLAY=:0

    Setting the DISPLAY run time environment variable fixes it for me — typical X Windows lossage, fails to default to your local display (designed for remote displays) which you'd only know if you'd gone to X11 summer camp.

    A complete working example in bash:

    (cd /tmp && g++ -xc++ - -lSDL2 && (DISPLAY=:0 ./a.out; echo \$? = $?)) <<.
    #include <SDL2/SDL.h>
    #include <iostream>
    
    int main() {
      if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        std::cout << SDL_GetError() << std::endl;
        return 1;
      }
      return 0;
    }
    .
    

    $? = 0

    0 讨论(0)
  • 2021-01-20 04:51

    I had this error when I was using gcc to compile. When I used g++ to compile it fixes the issue. The Lazy Foo tutorial also recommends you to use g++ to compile.

    If you are having this problem you can try using g++ to compile and see if this resolves the issue.

    0 讨论(0)
  • 2021-01-20 04:52

    Does the environment you are running on have a windowing system?
    This error has come up for me when I'm running tests initializing SDL2 on Travis CI using Ubuntu 14.04. Based on what I've been able to gather from further testing and hints I've gotten from google searches SDL_Init when initializing SDL if it is passed SDL_INIT_VIDEO (which you are doing implicitly with SDL_INIT_EVERYTHING), it will try and connect with the windowing system of your machine.
    So perhaps try:

    SDL_Init(0)
    

    and then initialize the other subsystems later with:

    SDL_InitSubSystem(SDL_INIT_EVERYTHING)
    

    If you do keep in mind that you must quit every subsystem you initialize in this case it's just technically. You would do that like this:

    SDL_QuitSubSystem(SDL_INIT_EVERYTHING)
    
    0 讨论(0)
提交回复
热议问题