2

I'm using g++ on WSL Ubuntu. I cloned the GLFW repo using git, used the ccmake command to configure and generate the binaries, then used make inside the "build" directory to finally create the .a file. I installed all the OpenGL-related libraries to /usr/ld (I don't recall exactly which I installed, since I had to install so many. Regardless, the g++ command works, so I assume it was a success). Afterwards, I made a project on VS Code that looks like this:

enter image description here

The GLFW include-folder is from the aforementioned cloned repo, and the GLAD and KHR include-folders are from glad.dav1d.de, where I set the GL version (under API) to 3.3, and the Profile to Core.

In main.cpp, I put a simple snippet of code meant to initialize GLFW:

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

int main()
{
    // Initialize GLFW
    if (!glfwInit()) {
        std::cerr << "Failed to initialize GLFW" << std::endl;
        return 1;
    }
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    std::cout << "Success" << std::endl;

    return 0;
}

Then used this to command to compile the project: g++ -Wall -I./include/ -L./lib/ src/*.{c,cpp} -lglfw3 -lGL -lX11 -lpthread -lXrandr -lXi -ldl -o main. There were no warnings or errors or anything. After running the executable, I get the error message that GLFW failed to initialize. I was a little confused why this happened, so I typed the glxinfo command to find out that my DISPLAY environment variable hasn't been set.

I have absolutely no idea what this variable is, or what it's meant to represent. After scouring around on the internet, I came across export DISPLAY=:0.0 and export DISPLAY=1 and export DISPLAY=IP:0.0, none of which worked. Any ideas on how I could resolve this?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Rapid Readers
  • 303
  • 1
  • 3
  • 13
  • 1
    `DISPLAY` indicates where the X display server is running. Instead of googling around for what `DISPLAY` server is set to, try Googling around for what it means and how it works, and maybe it'll point to a clue. Perhaps you're using a Wayland-only server without X display server support. – Sam Varshavchik Jan 01 '22 at 01:35
  • @SamVarshavchik Yeah, you're right. Apologies for not digging into this deeper. After looking around for explanations, I now have a better understanding of what it actually is. – Rapid Readers Jan 05 '22 at 03:57

2 Answers2

6

WSL on Windows 10 does not include support for GUI apps. You can build an app with OpenGL/X libraries, sure, but running it requires an X server on which to actually display it.

In general, you have 3 options. I believe all of these will work with OpenGL, although I have not tested each of them in that capacity:

  • The "supported" method is to use Windows 11 with WSLg. Windows 11 supports hardware-accelerated OpenGL in WSL2 out-of-the-box using a supported WDDMv3.0 driver (available for AMD, Intel, and nVidia GPUs). See Microsoft Docs for more information.

  • The "normal" method of running an X server on WSL with Windows 10 is to install a third-party X server such as VcXsrv. See this Super User question for some details on OpenGL support under that scenario. I don't believe that this will be hardware-accelerated, however.

    You'll need to manually configure the DISPLAY variable in this case to point to your Windows host. You'll find some fairly complicated directions for doing so, but IMHO the easiest way is via mDNS.

  • Before upgrading to Windows 11, I used xrdp when I needed X support. This allows access to the WSL instance using the Remote Desktop Protocol and supported apps like the built-in Windows Remote Desktop Connection. I don't believe there is a way to hardware accelerate OpenGL using RDP, either. In my opinion, this is far easier than setting up a 3rd party X server. See my answer on Ask Ubuntu for steps to enable.

TylerH
  • 20,799
  • 66
  • 75
  • 101
NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
  • Thank you! I went with the second method (and also followed this guide: https://techcommunity.microsoft.com/t5/windows-dev-appconsult/running-wsl-gui-apps-on-windows-10/ba-p/1493242) and so far so good. – Rapid Readers Jan 05 '22 at 03:54
1

WSL 2 now has support for GUI apps on Windows 11: https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

I had just installed the GPU driver, updated WSL, and now it works for me.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Wait, so is it possible to run your own OpenGL C++ applications using WSL now? – Rapid Readers Mar 20 '22 at 05:29
  • @Diego - Welcome to Stack Overflow. I'd encourage you to read the existing answers to questions before posting duplicate information. I already covered that Windows 11 WSL2 includes this capability in my previous answer. Also, your answer doesn't point out that it won't work on Windows 10. – NotTheDr01ds Mar 20 '22 at 13:33
  • @NotTheDr01ds It was probably an accident. I restored the accept now. Continue on with your day. – Rapid Readers Mar 21 '22 at 04:20
  • 1
    This essentially just repeats the existing answer. – TylerH Mar 21 '22 at 13:52
  • Thanks @RapidReaders - I have no problem with better answers coming along and displacing mine, but I was confused on this one since this (IMHO) wasn't a better answer. Happy to hear it wasn't intentional, and thanks for fixing it! – NotTheDr01ds Mar 21 '22 at 14:54
  • @NotTheDr01ds, sorry... I tried to find for an answer for days, and some day I could get it working by installing a program on windows and a lot of things in wsl, but when i formatted my pc, it didn't work anymore. When I found this driver on Microsoft docs, I tried just to help others and I just saw that you said that there is no support. I'll take more attention next time. I was just trying to help looking for questions with this problem. – Diego C. Rodrigues Mar 23 '22 at 03:40