Windows WSL for Programming C++
There are a number of ways to run VS Code with C++ under Windows. This guide discusses installing VS Code inside either the Windows Subsystem for Linux (WSL), or through a Virtual Machine.
Instead of using this WSL approach, is it easier to use a Dev Container instead.
VS Code in WSL
Windows has a "Windows Subsystem for Linux" (WSL) which allows you to run Linux, and Linux programs within Windows in a somewhat controlled way. The WSL is nice because you are running the compiler under Linux which is how we will test your assignments, and it is often faster than installing a full virtual machine. Using WSL gives you a smooth development environment, easy debugging, and strong assurances that your code will compile OK for marking.
There are two versions of WSL (1, and 2). WSL2 is much less likely to cause problems.
This video shows you an overview of VS Code for WSL; follow these steps to use VS Code in WSL:
Install WSL 2
Follow steps below, taken from WSL guide from Microsoft.
- Run PowerShell as Administrator
To do this, go to Start Menu, type PowerShell, select Run as Administrator. - Install WSL (may take a few minutes):
wsl --install- If asked to create a user, enter your first name (all lower case)
- If asked to create a password, enter a simple to remember password. When typing a password in, it will not show you anything as you type each key!
- Install the
UbuntuWindows App
From the Start menu, go to the Windows Store, and install theUbuntuapp. - Confirm correct version of WSL and Ubuntu are installed. Run PowerShell as Administrator and run
wsl --list -v- The output should look like:
PS C:\WINDOWS\system32> wsl --list -v
NAME STATE VERSION
* Ubuntu Running 2
- The output should look like:
Troubleshooting
- If WSL is Version 1 (WSL1), the run:
wsl --set-version Ubuntu 2- Rerun the
wsl --list -vcommand to ensure it's now version 2.
- Rerun the
- If WSL won't install, then you may need to enable some Windows features to allow WSL and virtual machines (because WSL2 is basically a built-in virtual machine):
- Open Start Menu, run Turn Windows Features On Or Off
- Ensure there are checkmarks beside:
- Virtual Machine Platform
- Windows Subsystem for Linux
- If WSL still won't install, ensure that your computer's BIOS (what runs at the very start of when the computer is turned on) allows virtual machines:
- Turn off your computer.
- Turn on your computer and enter BIOS. You likely need to press and hold F2 as soon as your computer turns on; but you may also need to press Delete or ESC, F1 or F4 depending on the system.
- Find and enable the setting for VT-x, AMD-V, SVM, or Vanderpool.
- See this brief guide for more.
- If WSL still won't install, try reopening the Turn Windows Features On or Off and enable Windows Hypervisor Platform
Install Tools in WSL
- Open the Ubuntu app (type Ubuntu in the Windows Start menu search box) which opens another command line interface. The prompt may look similar to:
brian@BrianPC-5900:~$ - In the Ubuntu terminal (not PowerShell) run the command to update the package manager:
sudo apt update
(Enter you password as needed; it won't show anything as you type.) - Install C++ compiler and debugger:
sudo apt install g++ gdb
Selectyesif asked to confirm. - Verify tools installed with the following commands in the Ubuntu terminal:
g++ --version
gdb --version - Create a CMPT130 directory for your work:
mkdir cmpt130
Install VS Code
Follow these steps to configure VS Code to use WSL, taken from this Microsoft guide.
- Into Windows, download and install VS Code.
- Open VS Code, and on the very left select the Extension tab, install the extension named
WSL. - Click the "><" icon in the very bottom left and select Connect To WSL
- File > Open Folder; select
cmpt130from the list, click OK.
Writing Your C++23 Code
Proceed now to configure VS Code for C++23 and write your code.
Go To Writing Your C++23 Code Configure VS Code and select C++23 when compiling / running / debugging.Submitting Code from WSL
Your C++ files are saved on your local computer, but in a special area for access inside WSL. With the above steps, we created the cmpt130 folder inside of WSL on your computer. This is in a different place than your normal Documents. To easily access this folder, do the following:
- Run Window's "File Explorer".
- In the bar at the top, type
\\wsl$- Or, from the start menu, type
\\wsl$and press Enter.
- Or, from the start menu, type
- Double click on your Ubuntu folder
- Double click on
home. - Double click on the folder named after you.
- Right-click the
cmpt130folder and drag-and-drop it onto your desktop. - Select "Create shortcut here"
- If you see a warning like "These files might be harmful to your computer", select OK.
Now, when you want to access your .cpp files, just use this shortcut on your desktop. You can use a normal web browser to browse to these files when you submit your work.
Alternative to WSL: VS Code in a Linux VM
A virtual machine (VM) is a program you run on your computer, much like any other program, which is basically a fake (virtual) computer. Into a VM you can install a different operating system and any programs you like. The VM runs inside Windows (in this case), so it will not interfere with anything you normally do on your computer (no need to reformat your computer, etc!). You can run all your normal programs (web browser, music app) at the same time as you run the VM.
If you got the WSL working above, then you don't need to install a VM. Only install a VM if you want to manage an additional Linux installed system. This is not the recommended way to work with VS Code in this course.
- First install a Virtual Machine program in Windows, and then create a virtual machine for Ubuntu Linux
- Installing VMWare Player and Ubuntu VM. Alternatively, you could install VirtualBox.
- Configuring a VMWare Player's virtual machine. You'll need to download Ubuntu (select the desktop "LTS" version).
- Next follow the directions for installing VS Code in Linux
- When you want to close your VM, you can click the X button and then select to save the VM state for next time (otherwise it's like pulling the plug on the virtual machine computer, which is not good for the system)
To get files onto or off of your Linux VM, you could use a web-browser to submit your assignments directly from the VM. Or, you can often drag-and-drop files between your VM (Linux) and your host OS (Windows); however, this is often buggy.
Here is another guide to installing Ubuntu (Linux) into a VM under Windows.
Notes
- It is possible to install the MinGW C++ compiler into Windows. This way is not very good because it is not possible to use the debugger for programs that read from the keyboard. Therefore, it is not recommended.
- These instructions are an abbreviated version of what appears on this Microsoft help page. For more details see the link in the previous sentence.