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

柔情痞子 提交于 2019-12-01 22:36:52

问题


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() returns "Failed to connect to the Mir Server". I am sure SDL is installed correctly since I can successfully initialize SDL on another project.

These are the libraries I am using: http://i.imgur.com/SS1mjzQ.png

#include <SDL2/SDL.h>
#include <iostream>

int main(int argc, char* args[]) {
  if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
    std::cout << SDL_GetError() << std::endl;
  }

  return 0;
}

回答1:


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




回答2:


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)



回答3:


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.



来源:https://stackoverflow.com/questions/25468976/sdl-2-0-wont-initialize-error-failed-to-connect-to-the-mir-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!