Skip to content

Instantly share code, notes, and snippets.

@ianelsbree
Last active April 30, 2024 17:11
Show Gist options
  • Save ianelsbree/9673c73709f4697978f4386736813147 to your computer and use it in GitHub Desktop.
Save ianelsbree/9673c73709f4697978f4386736813147 to your computer and use it in GitHub Desktop.
WSL with VS Code Setup

Setting Up WSL with VS Code for CS-120/CS-170

Introduction

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

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.

Getting WSL

  1. 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.

Windows Terminal

  1. 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.
  2. Open Windows Terminal in the same fashion as PowerShell.
  3. Select the drop-down arrow at the top, next to the new tab button, and select "Settings".
  4. 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.
  5. Restart Windows Terminal.

Setting up WSL

  1. Follow the on-screen instructions to set up the base system.
  2. 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.
  3. 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>
    • Wait for the installation to complete (enter Y when/if prompted), then continue.

Using WSL

  • 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 path
    • valgrind <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 and g++
      • Running your executable with ./<Executable Name>

Visual Studio Code

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.

Getting VSCode

  1. Go to https://code.visualstudio.com/ and install Visual Studo Code.
  2. Open it up. You now have Visual Studio Code.

Setting up VSCode

  • All of these steps are optional, but recommended.
  1. 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.
  2. Install a color theme from the Extensions tab.
    • Just for fun!
  3. Take a look at the default keybindings, and change any that you want to.
    • Press Ctrl+K then Ctrl+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.

Using VSCode

  1. 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.
  2. 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.

Using WSL with VSCode

  1. 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.
  2. 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!
  3. Click the dropdown arrow again, and then "Select Default Profile"
  4. Select "Debian (WSL)" in the menu that appears at the top.
    • Now your Debian WSL will be your default terminal in VSCode.
  5. 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 and g++. 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.

Workflow

  • 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 use make to compile, run, and test the code you just wrote.
  • If you're not using makefiles, your regular compilation commands will still work.
    1. Use gcc for C code or g++ for C++ code, along with some of my favorite flags: -O -Wall -Wextra -ansi -pedantic and -o to specify the output file.
    2. Use ./<Executable Name> [Arguments] to run and test your program, using diff to check output differences from expected output.
  • You can also use VSCode's diff view mode to make debugging easier!
    1. In the Explorer tab on the left, select (Ctrl+Click) the files you want to compare.
    2. Right-click one of the files, then click "Compare Selected".

Check out my primer on make and makefiles here


By Ian Elsbree, 2022-02-11
Last Updated: 2022-09-18
@iceblue64
Copy link

Very helpful. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment