1

I've installed WSL2 on Windows 10 along with GLFW. When I try to run the default GLFW example program, I run into the following error X11: The DISPLAY environment variable is missing. I build the program using this command-line:

g++ main.cpp -lglfw -lGL -o main

Program:

#include <stdio.h>
#include <iostream>
#include <GLFW/glfw3.h>


static void glfwError(int /*id*/, const char* description)
{
    std::cerr << description << '\n';
    exit(0);
}

int main(void)
{
    GLFWwindow* window;
    glfwSetErrorCallback(&glfwError);

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

How can I fix this issue?

spaL
  • 604
  • 7
  • 21
  • Does this answer your question? [How to set up working X11 forwarding on WSL2](https://stackoverflow.com/questions/61110603/how-to-set-up-working-x11-forwarding-on-wsl2) – brc-dd Sep 15 '21 at 05:34
  • 1
    Put this in shell before running your program `export DISPLAY=:0;` if using WSLg (should be there by default though), else do as indicated in the linked question. – brc-dd Sep 15 '21 at 05:35
  • Now the error it gives is `X11: Failed to open display 0`. Do I have to install X server for Windows 10? – spaL Sep 15 '21 at 05:37
  • 1
    You are probably not running the X11 server on your Windows or your firewall is blocking access to it. So you need to follow the resolutions given in the linked question. PS: **_YES_** you have to install X11 server, WSL had no graphics support till recent versions ([WSLg](https://github.com/microsoft/wslg) is available on Insider builds 21362+). – brc-dd Sep 15 '21 at 05:39
  • Oh ok, I'll try to follow the link you posted earlier. Do you have a recommendation for an X Server (e.g. VcXsrv or XMing or X410) or does it not matter? – spaL Sep 15 '21 at 05:49
  • It doesn't matter. Although IIRC X410 and XMing are both paid and may provide more features or better integration with WSL. I have used VcXsrv though. It works just the fine. – brc-dd Sep 15 '21 at 05:57
  • I've got it working where after I compile it and run it using ./App, the window pops up. However it seems that I can only close the window by terminating the app through the terminal. Also, when I drag the window around its really laggy. I know these are just minor problems now but are there any possible solutions? – spaL Sep 15 '21 at 09:10
  • Never experienced lag, so can't comment on that. Can be due to hardware or WSL configuration. But obviously, WSL won't work as fast as Windows or Linux booted directly from the drive. For closing the window you can simply handle the escape key press event by putting `if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true);` in your render loop. BTW, you should be getting the close (X) icon. Not sure why you aren't getting it. – brc-dd Sep 15 '21 at 09:39
  • I am getting the close (X) icon. I've modified the code a bit so that it prints a number counting up in every iteration of the while loop. Even before I do anything, the count stops at ~8000, thus I believe the program is continuously crashing before I can even press the (X) icon. This may have some correlation to why it is so laggy when I drag it around. Not sure how to fix this :/ – spaL Sep 15 '21 at 09:58
  • Why are you counting it? The render loop executes multiple times in a single second. This is the expected behavior. Nothing is wrong with it. Your program is crashing because of other reasons. Ask a new question with all the details. This question is clearly resolved as you are no longer getting X11 errors. For me, I cannot reproduce your issue as I am on dev channel and have WSLg which doesn't use X11. Just a guess, crash may be because of improper OpenGL pointers. – brc-dd Sep 15 '21 at 10:35
  • 1
    Does this answer your question? ["Error: Unable to open display", even after manually setting display environment variable](https://stackoverflow.com/questions/70546571/error-unable-to-open-display-even-after-manually-setting-display-environment) – NotTheDr01ds Mar 20 '22 at 20:50

0 Answers0