-1

Here is my simple program

#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    SDL_Window *window = NULL;

    if(SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        SDL_Log("ERROR : SDL Initialization > %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    //Program
    window = SDL_CreateWindow("My Viewer",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
    if(window == NULL)
    {
        SDL_Log("ERROR : SDL Window creation > %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    SDL_Delay(5000);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return EXIT_SUCCESS;
}

When running the command line : gcc main.c -o prog $(sdl2-config --cflags --libs) && ./prog

it display :

error: XDG_RUNTIME_DIR not set in the environment.

INFO: ERROR : SDL Initialization > No available video device

I search many forum and tried what they told us to do but nothing work.

I uninstall and re install (manually and also with command line) SDL2, change the env var "export DISPLAY=:0.0" or "export DISPLAY=:1", tried this forum but nothing seems to change.

I'm running this code in bash Ubuntu with WSL2.

huangkaiyi
  • 73
  • 2
  • 9
  • 1
    Which X server are you using? – genpfault Mar 09 '22 at 12:09
  • what is a X server, I don't really understant ... – huangkaiyi Mar 09 '22 at 12:57
  • @huangkaiyi What is your Windows version? And can you run other GUI applications under WSL2? Try installing `xterm` and running it. If it's not able to run, then you are probably on Windows 10, but regardless, this would fall into a "general computing" problem rather than "programming". See [this question](https://superuser.com/q/1580610/1210833) on Super User for details. Thanks! – NotTheDr01ds Mar 09 '22 at 14:48
  • I'm only guessing from previous comments, but chances are that you want to tell SDL that you aren't using X11... Because Ubuntu under WSL2 appears to be 22.04, which comes with Wayland instead. `SDL_VIDEODRIVER=wayland ./prog` –  Mar 09 '22 at 14:51
  • 1
    @Sahsahae The GUI support in WSL (which is only available in Windows 11) provides its own Wayland server, but it also enables Xwayland by default. Most likely the OP isn't running Windows 11, which means they either need to upgrade, run an X server under Windows, or use XRDP. – NotTheDr01ds Mar 09 '22 at 14:54
  • thanks all for your comment, I just did an update of windows 10 to 11, installed xterm and the "xeyes" didn' work ("error can't open display : 0"). Tried Xming and run Xming and also the same error. I'm keep searching for why it didn't work ... – huangkaiyi Mar 09 '22 at 16:44
  • @huangkaiyi After updating to Windows 11, make sure you also run `wsl --update` to bring in the new WSL features like GUI support. – NotTheDr01ds Mar 09 '22 at 16:46
  • @huangkaiyi Just for reference, I was able to run your sample code above successfully on WSL2 under Windows 11. – NotTheDr01ds Mar 09 '22 at 16:46
  • Did you by any change build SDL2 yourself? – HolyBlackCat Mar 09 '22 at 17:29

1 Answers1

0

Thank you everyone I manage to run my program with this solution :

  • Install Windows 11 (because Windows 10 is not able to run graphical app).

At This point, the following is run in the bash.exe

  • Install Xming and run Xming. (you can try running xeyes by installing xterm to check if it's working)
  • For SDL, just install sdl normally (sudo apt-get install libsdl2-2.0)
  • Run your program.

I didn't change any env var or any other library.

huangkaiyi
  • 73
  • 2
  • 9