This instructional guide will show you how to set up Windows Subsystem for Linux, along with Visual Studio Code integration; this will make writing, compiling, and testing code for the CS-120 and CS-170 courses more streamlined. I assume you are using the latest version of Microsoft Windows, have access to the Microsoft Store (only 1 free program is used), and have a basic knowledge of how to use the Windows operating system and command line. Prior knowledge of Linux and the Bash shell is not required, although it certainly doesn't hurt.
Windows Subsystem for Linux is actually a Linux subsystem on your Windows system. Confusing names aside, it allows you to run Linux programs (such as Valgrind) on a Windows computer. Additionally, Linux shell syntax is preferred by some, including those who may need to be running Windows for other reasons. As such, WSL allows a good compromise between Linux functionality and Windows compatibility.
- Follow this guide from Microsoft: https://docs.microsoft.com/en-us/windows/wsl/install
- I will assume a
Debian
install for the rest of this guide.
- I will assume a
- Open the Microsoft Store and install the app "Windows Terminal" by Microsoft Corporation.
- Advanced: This specific terminal is not strictly required, but it's easy to install and does the job just fine. Feel free to use your favorite terminal emulator otherwise.
- Open Windows Terminal in the same fashion as PowerShell.
- Select the drop-down arrow at the top, next to the new tab button, and select "Settings".
- In the "Startup" (default) section, change the "Default profile" to "Debian".
- It should have a little penguin icon next to it. That's Tux, the penguin mascot of linux.
- Restart Windows Terminal.
- Follow the on-screen instructions to set up the base system.
- Enter the command
sudo apt update && sudo apt upgrade
and enter your admin password when prompted.- Wait for the system update to complete (enter
Y
when/if prompted), then continue.
- Wait for the system update to complete (enter
- Download some programs you'll need for the course.
- Enter the command
sudo apt install build-essential valgrind diff
- This is also the command to use if you want/need to install more programs later:
sudo apt install <Program names separated by spaces>
- This is also the command to use if you want/need to install more programs later:
- Wait for the installation to complete (enter
Y
when/if prompted), then continue.
- Enter the command
- You can now access your new WSL installation by simply opening Windows Terminal.
- You can set Windows to use Windows Terminal as your default terminal in the Windows terminal settings, if you like.
- The commands you need to know for the course are:
cd <path>
- changes the directory you're looking at to the specified pathvalgrind <Executable Path>
- runs a memory test on your executable- You should already know these ones. If you don't, go look in your course materials.
make
gcc
andg++
- Running your executable with
./<Executable Name>
Now you have WSL, and can use that on its own in the Windows Terminal. Great, but what if you want to integrate it with Visual Studio Code? Luckily, the VSCode devs thought of this, and they made it pretty easy.
- Go to https://code.visualstudio.com/ and install Visual Studo Code.
- Open it up. You now have Visual Studio Code.
- All of these steps are optional, but recommended.
- Using the Extensions tab on the left, install the extension "C/C++" by Microsoft.
- This extension includes IntelliSense for C and C++, as well as automatic formatting support.
- Install a color theme from the Extensions tab.
- Just for fun!
- Take a look at the default keybindings, and change any that you want to.
- Press
Ctrl+K
thenCtrl+S
to open the keybindings menu. This type of shortcut is called a "chord". - Another good keybind to know is
Ctrl+,
, which opens the settings menu.
- Press
- When opening a project, use the "Open Folder" feature of VSCode, accessible through the File menu or
Ctrl+K Ctrl+O
. This allows you to interact with all files in the folder easily, and makes the WSL integration part a little simpler. - VSCode is primarily a text editor, like Notepad++, Atom, Vim, or whatever other text editor you like. However, it is extensible by extensions from the built-in marketplace, as well as what we're about to do with a terminal.
- Press
Ctrl+`
(the un-shifted character of the tilde~
key).- This opens/closes the VSCode Terminal.
- If you right-click near the top of the terminal, you can select "Move Views to Side Panel" if you want.
- There will be a dropdown arrow next to the Split Terminal and Kill Terminal buttons. In its menu, select "Debian (WSL)".
- Now you have your WSL in VSCode!
- Click the dropdown arrow again, and then "Select Default Profile"
- Select "Debian (WSL)" in the menu that appears at the top.
- Now your Debian WSL will be your default terminal in VSCode.
- Enjoy!
- If you used "Open Folder" in VSCode, then WSL should start in your project folder by default.
- All WSL commands will work the same as normal, including
gcc
andg++
. They work the same as when used in CMD. - Remember that Linux uses
/
in file paths, not\
like windows. - Use
make
along with makefiles to have the most expedient compiling, running, and testing.
- If you're using makefiles, you can just write the code you need in the text editor, press
Ctrl+`
to open the terminal sidebar, and usemake
to compile, run, and test the code you just wrote. - If you're not using makefiles, your regular compilation commands will still work.
- Use
gcc
for C code org++
for C++ code, along with some of my favorite flags:-O -Wall -Wextra -ansi -pedantic
and-o
to specify the output file. - Use
./<Executable Name> [Arguments]
to run and test your program, usingdiff
to check output differences from expected output.
- Use
- You can also use VSCode's diff view mode to make debugging easier!
- In the Explorer tab on the left, select (
Ctrl+Click
) the files you want to compare. - Right-click one of the files, then click "Compare Selected".
- In the Explorer tab on the left, select (
Very helpful. Thank you!