This guide provides step-by-step instructions on how to set up Windows Subsystem for Linux (WSL) for C/C++/OpenGL development. By the end of this guide, you should have a fully functional development environment for creating C/C++/OpenGL applications using WSL2 on Windows 10/11.
Before you begin, make sure you have the following prerequisites installed on your Windows 10 machine:
- For x64 systems: Version 1903 or later, with Build 18362 or later.
- For ARM64 systems: Version 2004 or later, with Build 19041 or later.
or Windows 11.
Follow the steps below to install WSL2 on your Windows 10/11 machine:
-
Open PowerShell as an administrator.
-
Run the following command to enable the Windows Subsystem for Linux feature:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
-
Restart your computer when prompted.
-
Download and install the WSL2 Linux kernel update package for x64 machines
-
Open PowerShell as an administrator again.
-
Run the following command to set WSL2 as the default version:
wsl --set-default-version 2
-
Open the Microsoft Store and search for your preferred Linux distribution (e.g., Ubuntu, Debian, or Kali Linux). Click "Get" to install the distribution on your WSL. We suggest installing Ubuntu 22.04.2 LTS.
-
Now Install Windows Subsystem for Linux(WSL) lets developers run a GNU/Linux environment -- including most command-line tools, utilities, and applications -- directly on Windows, unmodified, without the overhead of a traditional virtual machine or dual boot setup.
-
Launch and configure Linux distribution: Open the Start menu and click on the Linux distribution you installed to launch it. Follow the on-screen instructions to set up the Linux distribution.
Install development tools: In the Linux terminal, run the following command to install the necessary development tools:
sudo apt update && sudo apt install build-essential mesa-utils libglu1-mesa-dev freeglut3-dev mesa-common-dev libglew-dev libglfw3-dev libglm-dev libao-dev libmpg123-dev -y
This command installs the build-essential package, which includes compilers and other tools for building C/C++ applications, as well as the mesa-utils and libgl1-mesa-dev packages, which are necessary for OpenGL development.
In addition to using the terminal to write and compile code, you can also set up an Integrated Development Environment (IDE) to make the development process smoother. Here are steps to set up two popular IDEs, Visual Studio Code(Windows Native) and Gedit(WSL GUI):
-
Download and install Visual Studio Code.
-
Open Visual Studio Code and install the Remote Development extension. This extension allows you to develop in a remote environment, such as a WSL distribution.
-
Connect Visual Studio Code to WSL2: In Visual Studio Code, click on the Remote Explorer icon on the left-hand side. Click on the "Connect to a remote host" button and select "WSL: Ubuntu" (or the Linux distribution you installed). Visual Studio Code will then connect to your WSL2 instance.
-
Create a new C/C++/OpenGL project: In Visual Studio Code, create a new folder for your project. Open a terminal window in Visual Studio Code (Ctrl + Shift + `) and navigate to your project folder. Use your preferred method to create a new C/C++/OpenGL project (e.g., using a code template or by creating a new file).
-
Build and run your project: Use the terminal in Visual Studio Code to build and run your C/C++/OpenGL project.
- Install Gedit in WSL: Open the terminal and run the following command:
sudo apt update && sudo apt install gedit
- Open Gedit: In the terminal, run the following command to open Gedit:
gedit
-
Create a new C/C++/OpenGL project: In Gedit, create a new file for your project. Use your preferred method to create a new C/C++/OpenGL project (e.g., using a code template or by creating a new file).
-
Build and run your project: Use the terminal in WSL to build and run your C/C++/OpenGL project.
This section will guide you through creating and compiling C, C++, and OpenGL files using the terminal in WSL.
-
Create a new file with the extension
.c
(ex. hello.c). -
Write your C code in the file, such as the following "hello world" program:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
-
Save the file.
-
In the terminal, navigate to the directory where the file is located.
-
Compile the C file using the following command:
gcc -o hello hello.c
- Run the compiled program using the following command:
./hello
-
Create a new file with the extension
.cpp
(ex. hello.cpp). -
Write your C++ code in the file, such as the following "hello world" program:
#include <iostream>
int main() {
std::cout << "Hello, world!\n";
return 0;
}
-
Save the file.
-
In the terminal, navigate to the directory where the file is located.
-
Compile the C++ file using the following command:
g++ -o hello hello.cpp
- Run the compiled program using the following command:
./hello
-
Create a new file with the extension
.c
(ex. triangle.c). -
Write your OpenGL code in the file, such as the following "triangle" program:
#include <GL/gl.h>
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5, -0.5);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(0.5, -0.5);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(0.0, 0.5);
glEnd();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("OpenGL Test");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
-
Save the file.
-
In the terminal, navigate to the directory where the file is located.
-
Compile the OpenGL file using the following command:
gcc -o triangle triangle.c -lGL -lglut
- Run the compiled program using the following command:
./triangle
Congratulations! You now have a fully functional development environment for C/C++/OpenGL development using WSL2 on Windows 10/11, complete with an IDE of your choice.