Using Visual Studio Code for C in CSC173
Nora Goodman Fall 2023
Included VSCode features that make it work well for CSC173
- Convenient to keep a similar development environment across languages — e.g. if a student used VSCode for Java development in 172, they can keep using most of the same extensions, debugger, VCS, layout, etc. as the previous semester.
- Cross-platform
- Good integration with Windows Subsystem for Linux (WSL)
- Very extendable
- Free and open source, no worrying about licensing
- Lightweight
Disadvantages to using VSCode for 173
- Takes some setup
- My preferred way of setting it up on Windows is to use WSL, as this makes it easy to use GDB. Maybe someone who is better at Windows can support setup without installing WSL, but I find it easiest. WSL can take some time to setup although it is very straightforward.
- While there is a decent debugger, there were a couple times I would have liked more features in visualizing how the memory is stored. It is possible that Visual Studio has a better C debugger, although that only runs on Windows and misses a lot of the advantages of VSCode.
Setting up VSCode
Vscode’s magical keyboard shortcut
- From any page in the editor you can use the keyboard shortcut “ctrl-shift-P”. (or command if you’re on mac, but for the rest of this guide I’m just gonna say ctrl) From there, you can type a couple words about what you want to do and it’ll let you select that action. You will use this shortcut frequently in this guide.
Windows Instructions
Note: These instructions are basically just getting WSL setup which will take a while. But, you’ll have it all set up and will be very convenient for development in any other language.
What is WSL?
From Wikipedia:
Windows Subsystem for Linux (WSL) is a feature of Windows that allows developers to run a Linux environment without the need for a separate virtual machine or dual booting.
If you’ve never used Linux, it’s just an operating system like Windows or Mac, but it’s free and good for programming. You’ll be using a feature of Windows that creates a Linux environment.
“Why should I use it for this course?”
- Most versions of Linux come installed with a “package manager”. On Windows, you need to locate the installer online for every single little program. Package managers keep a database of most programs you might need and it makes it super simple to install a few things at once with just a few terminal commands.
- It has very good integration with GNU tools which are useful for C development
- The TAs will likely run your code on a Linux machine as well
Installing WSL
- This is a link with instructions to install and setup WSL but long story short you just pop open Powershell and type
wsl --install
. It will use a recent version of Ubuntu as the default distribution, which will be fine for this course. Give it a username and password.- Note: As you’re typing out a password you won’t see anything show up. This is normal on Linux systems, just type your password and press enter.
A couple Linux commands you’ll need to run
The fastest way to get a lot of software updated and installed onto your Linux environment is by using the package manager in your command line.
First you’ll need to update all the software on your Linux environment.
In your terminal, type sudo apt update
Here’s what that means:
sudo
stands for “Superuser Do”, which basically means “run as admin.” Only use asudo
command if you’re sure about what you’re doing.
apt
is your package manager. This is the program that will let you install and update software. You may also seeapt-get
in some tutorials, which is mostly interchangeable.
update
means “check for new versions of software.” This doesn’t yet actually change what you currently have installed.
Once that’s ready, you can type sudo apt upgrade
, which will upgrade all the software in your Linux environment.
Now we need to install the tools we need for C development. To do this, type:
sudo apt install gcc gdb make valgrind
Here’s what you just installed:
gcc | The “GNU Compiler Collection”, a popular free C compiler |
gdb | The “GNU Debugger”, a popular C debugger |
make | A tool that will ensure consistency with compiling everything together |
valgrind | A program that will let you check for memory leaks before you submit your assignment (optional, but it’s a nice sanity check if you have time to get it done) |
Set up VSCode for WSL
- Follow the guide here. Make sure to read the section about “Extensions inside of VS Code WSL” — understanding this is pretty important.
Instructions for Windows and Mac now that everyone is on the same page
Install necessary VSCode extensions
On the left toolbar of VSCode, you’ll see an icon for some building blocks. That’s where you can install extensions.
Search for and install the “Makefile Tools” extension. It’s this one: https://marketplace.visualstudio.com/items?itemName=ms-vscode.makefile-tools
Also, get the C/C++ extension pack:
https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools-extension-pack
Demo project
Download this Zip file, extract it, and open the folder with VSCode.
Note: When you have VSCode open and you click “Open Folder”, you’ll see your Linux system’s files, not the normal files of your computer. You can go to the directory /mnt/c
to get to your files.
Take a look at the file named Makefile
. This is a set of instructions that your computer will run in order to compile the project. The really cool thing here is that basically any computer that has make
will run it the exact same way. This means that, when you submit your project, the only thing the TA has to do is run make
, which will both ensure consistency and also make your TA happy 🙂. You can reuse this Makefile for other projects, but you will need to set the executable name (the line where it says PROG
, change hello_world
to the name of your project) and your source file names (where it says SRCS
, list all of your C files).
Note that the Makefile has the recommended C flags to be used for this course. These ensure consistency with how the TAs compile it. If your code compiles with these C flags set, it should compile properly on a TA’s computer.
Using the Makefile tools extension
After installing Makefile tools, go to the “run and debug” section of the VSCode sidebar. You should see the category, MAKEFILE: PROJECT OUTLINE
. To run your code, hover over this category and hit “play.” The output of the program will likely be in the “Terminal” section on the bottom bar.
You can also use the debugger by clicking the little bug icon next to the play button. The debugger will save your life in this course. See this page on how to use the debugger if it’s new to you — the only sections you really should care about are the “debug actions” and the “breakpoints” section.