EECS 280 Tutorials
Setup up VS Code for C/C++
Visual Studio Code is a lightweight, easy-to-use, source code editor with debugging support. It runs on macOS, Windows, and Linux (including CAEN Linux). Visual Studio Code is not the same program as Visual Studio.
If you already have VS Code installed with the C/C++ extensions, skip to the Create a project section.
Prerequisites
VS Code relies on external command line tools. If you haven’t installed CLI tools on your machine yet, follow one of these tutorials first.
macOS | Windows | Linux |
Make sure you have a compiler and a debugger installed. Your version might be different. Instructions for installation on macOS, Windows/WSL/Linux.
$ g++ --version # macOS
Apple clang version 13.1.6 (clang-1316.0.21.2.5)
$ lldb --version # macOS
Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12)
$ g++ --version # WSL/Linux
g++ (GCC) 8.5.0 20210514
$ gdb --version # WSL/Linux
GNU gdb (GDB)
Next, follow our Command line interface (CLI) tutorial.
Restart
To start clean, first quit VS Code. Back up your files, and then delete your project directory. Your project directory might be different.
$ pwd
/Users/awdeorio/src/eecs280
$ cp -a p1-stats p1-stats.bak # Backup
$ rm -rf p1-stats # Delete
Install
Choose your platform below. Also make sure to install extensions.
Linux
Install the .deb package from the web https://code.visualstudio.com/docs/setup/linux.
CAEN Linux
VS Code is already installed on CAEN Linux desktop environment. You can use it while sitting at a CAEN Linux computer, or through a VNC connection to CAEN Linux.
macOS
Make sure you have macOS 11.1 or later.
$ sw_vers
ProductName: macOS
ProductVersion: 11.7
Use the homebrew package manager to install VS Code. You can run this command from any directory.
$ brew install --cask visual-studio-code
Windows
Make sure you have updated Windows and WSL installations according to the WSL tutorial.
Then, Install VS Code from the web https://code.visualstudio.com/.
Select “Add to PATH”.
Reboot. Open a terminal again (WSL/Ubuntu) and verify your installation. Your version might be different.
$ code --version
1.74.1
1ad8d514439d5077d2b0b7ee64d2ce82a9308e5a
x64
Extensions
Make sure VS Code is installed correctly by checking the version. You need version 1.52.1 or higher.
$ code --version
1.52.1
macOS
Install the Microsoft C/C++ extension and the CodeLLDB extension. See the C/C++ extension alternatives section for details about why we recommend these extensions.
$ code --install-extension ms-vscode.cpptools
$ code --install-extension vadimcn.vscode-lldb
Restart VS Code.
Verify that the extensions are installed. It’s OK if you have other extensions installed.
$ code --list-extensions
ms-vscode.cpptools
vadimcn.vscode-lldb
Windows
Install the Microsoft C/C++ extension and the WSL extension. See the C/C++ extension alternatives section for details about why we recommend these extensions.
$ code --install-extension ms-vscode.cpptools
$ code --install-extension ms-vscode-remote.remote-wsl
Restart VS Code.
Verify that the extension is installed. It’s OK if you have other extensions installed.
$ code --list-extensions
ms-vscode.cpptools
ms-vscode-remote.remote-wsl
Create a project
To create a VS Code project, create a folder (directory). There are many ways to create folders: Finder AKA File Explorer, VS Code interface, VS Code integrated terminal, and the system terminal. We’ll use the system terminal and call our example project p1-stats
.
Open the Terminal (macOS) or Ubuntu Bash Shell (Windows).
Navigate to your home directory, create a new directory, then move into the new directory. Your folder location might be different. Here’s some help with cd
, the tilde ~
, and mkdir
.
$ mkdir ~/eecs280
$ cd ~/eecs280
$ mkdir p1-stats
$ cd p1-stats
Pitfall: Avoid paths that contain spaces. Spaces causes problems with some command line tools.
Bad Example | Good Example |
---|---|
EECS 280/ |
eecs280/ |
Project 1 Stats/ |
p1-stats/ |
Windows Pitfall: Linux (Ubuntu) has a separate home directory. Storing code in your Windows home directory can cause slowdowns.
$ pwd
/home/awdeorio ... # Good, Linux home
/c/mnt/Users/awdeorio ... # Bad, Windows home
Here’s how to access your Linux files from Windows.
Start VS Code and open your project folder by selecting File
> Open Folder...
> navigate to the p1-stats
folder.
Pro-tip: Here’s a quick way to open VS Code to a specific project folder from the command line. First make sure you’re in the directory that contains your source code.
$ ls
main.cpp ...
$ code .
Add new files
Open your project folder by selecting File
> Open Folder...
> navigate to the p1-stats
folder.
Select the add file icon and give it a name, e.g., main.cpp
.
Alternatively, create your main.cpp
file from the command line using touch
.
$ touch main.cpp
Copy-paste this Hello World program into your main.cpp
.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!\n";
}
Add existing files
If you have starter files, add them to your project directory. This example is from EECS 280 Project 1, but this tutorial doesn’t require understanding the files. Your URL or files might be different.
Pitfall: Make sure you’re in the directory containing your source code.
$ ls
main.cpp
Pro-tip: Copy/paste instructions for WSL.
We’ll use the terminal to download, unpack, and move the starter files into the directory that already contains main.cpp
. Your URL or folder might be different.
$ wget https://eecs280staff.github.io/p1-stats/starter-files.tar.gz
$ tar -xvzf starter-files.tar.gz
$ mv starter-files/* .
$ rm -rf starter-files starter-files.tar.gz
You should see your new files in your project directory.
$ tree
.
├── Makefile
├── main.cpp
├── main_test.in
├── main_test.out.correct
├── main_test_data.tsv
├── p1_library.cpp
├── p1_library.h
├── stats.h
├── stats_public_test.cpp
└── stats_tests.cpp.starter
You should see your new files appear in VS Code.
Rename files
If you need to rename any files, you can do this from VS Code or from the command line. In EECS 280, you’ll need to rename any files that end in .starter
.
Right click a file and select “rename”. Change the file name. In EECS 280, you’ll do this to any file that ends in .starter
.
![]() |
![]() |
Pro-tip: You can also rename files the command line, for example:
$ mv stats_tests.cpp.starter stats_tests.cpp
Compile and Run
VS Code uses an executable you build at the command line.
First, compile and run your executable at the command line.
$ touch stats.cpp # Needed for EECS 280 P1
$ make main.exe
$ ./main.exe
Hello World!
Pitfall: Make sure you’re in the directory containing your source code.
$ ls
main.cpp ...
Pitfall: If you’re in EECS 280 and get an error like this, add a new file stats.cpp
. It’s OK if the file is empty for now.
$ make main.exe
make: *** No rule to make target `stats.cpp', needed by `main.exe'. Stop.
Pitfall: VS Code debugging will fail if there are no debugging symbols. Double check the output of make
and verify that you see -g
being used in the commands. The EECS 280 defaults include -g
.
$ make main.exe
g++ ... -g main.cpp ...
If you don’t have a Makefile
, you can compile manually. We don’t recommend this for EECS 280 students.
$ g++ -g main.cpp -o main.exe
macOS create launch.json
Select the file you would like to run. Navigate to the debugging pane.
Click “create a launch.json file”.
Select LLDB.
Edit the program
field in launch.json
. Save the updated file. Your program
name might be different.
Windows/WSL create launch.json
WSL Pitfall: Make sure you’re in WSL mode.
If you accidentally open VS Code from Windows mode, click on the green icon in the lower left hand corner and then select “Reopen Folder in WSL”.
Select the file you would like to run. Navigate to the debugging pane.
Click “create a launch.json file”.
Select C++ (GDB/LLDB).
Click “Add Configuration”.
Select the “C/C++ (gdb) Launch” configuration. This will create a default launch.json
(Microsoft Reference).
Edit the program
and cwd
fields in launch.json
. Save the updated file. Your program
name might be different.
Edit launch.json
program
If you already have a working launch.json
and want to debug a different program, edit the program
field launch.json
. Your program
name might be different. Make sure cwd
is set to "${workspaceFolder}"
.
{
"program": "${workspaceFolder}/main.exe",
...
"cwd": "${workspaceFolder}",
}
Run
Click the triangle to run. You’ll see your program’s output in the debug console.
Pitfall: Remember to build your executable at the command line first.
$ make main.exe
Sanitizers
We recommend enabling the address sanitizer and undefined behavior sanitizer. These will help you find memory errors like going off the end of an array or vector.
First, edit your Makefile
and add the CXXFLAGS
recommended by the ASAN Quick Start.
Then, edit the "environment"
property in your launch.json
. If there’s already an empty "environment": []
, replace it.
"environment": [
{
"name": "ASAN_OPTIONS",
"value": "abort_on_error=1:detect_leaks=0"
}
]
When ASan detects an error, VSCode will stop so that you can see the stack trace and inspect the current state of the program. This configuration also turns off leak-checking (LSan), which can’t run simultaneously with the visual debugger. If you do want to check for leaks, just run from the terminal with sanitizers enabled.
If you’re debugging something else in your program and don’t want it to terminate on ASAN errors, you can change to abort_on_error=0
.
Input redirection
Skip this subsection your first time through the tutorial. You can come back to it.
If you’re unfamiliar with input redirection, first read the CLI tutorial section on input redirection.
Windows/WSL or Linux launch.json
changes
To configure input redirection, edit launch.json
. These changes are for the Microsoft C/C++ extension.
{
"configurations": [
{
...
"program": "${workspaceFolder}/main.exe",
"args": ["<", "main_test.in"],
...
}
]
}
macOS launch.json
changes
To configure input redirection, edit launch.json
(docs). These instructions are for the CodeLLDB extension.
{
"configurations": [
{
...
"program": "${workspaceFolder}/main.exe",
"stdio": ["main_test.in", null, null],
...
}
]
}
Pitfall: Make sure you’re using the CodeLLDB extension. You should see lldb
in your launch.json
. If not, delete your launch.json
and try the compile and run section again.
{
"configurations": [
{
"type": "lldb",
...
Arguments and options
Skip this subsection for EECS 280 project 1.
Arguments and options are inputs to a program typed at the command line. Here’s an example from EECS 280 Project 5:
$ ./main.exe train_small.csv test_small.csv --debug
main.exe
is the name of the programtrain_small.csv
andtest_small.csv
are arguments--debug
is an option
To run a program with options or arguments in VS Code, edit launch.json
. Each option or argument should goes in a separate comma-separated string.
{
"configurations": [
{
...
"program": "${workspaceFolder}/main.exe",
"args": ["train_small.csv", "test_small.csv", "--debug"],
...
}
]
}
Debug
In this section, we’ll set a breakpoint, which pauses the debugger. Then, we’ll cover some of the options to continue execution.
Step Over
Run one line of code, stepping over any function calls by running the whole function in one step.
Step Into
Run one line of code, stepping into any function calls to execute them line-by-line.
Step Out
Run the program until it returns from the current function (or until the next breakpoint).
Continue
Run the program until the next breakpoint.
Example code
To get started, copy this example main.cpp
into your editor.
#include <iostream>
#include <vector>
using namespace std;
double sum (const vector<double> &data) {
double total = 0;
for (size_t i=0; i<data.size(); ++i) {
total += data[i];
}
return total;
}
int main() {
vector<double> data;
data.push_back(10);
data.push_back(20);
data.push_back(30);
cout << "sum(data) = " << sum(data) << endl;
}
Breakpoint
Select the file you want to debug. Set a breakpoint by clicking to the left of a line number. A breakpoint tells the program to pause.
Run
Select the debugging pane, then run the debugger. The program pauses at the breakpoint. The yellow indicator highlights the next line of code to be run.
Pitfall: Don’t forget to compile!
$ make main.exe # With a Makefile
$ g++ -g main.cpp -o main.exe # Without a Makefile
Step over
Click “Step Over” a few times until you reach the highlighted line of code
Inspect
Hover over a variable to inspect its value. You can also see values in the VARIABLES pane.
If you have trouble viewing the contents of a container like this screenshot, see Pretty Printing STL Containers with gdb
.
Step into
Click “Step Into”. The cursor enters the sum()
function.
Step out
Click “Step Out”. The sum()
function completes, and the program pauses again.
Continue
Press “Continue” to run the program to the next breakpoint, or the end, whichever comes first.
Troubleshooting
This section is for common problems and solutions.
Compile and run
If you have trouble with the compile and run section, a good first step is to delete your launch.json
and try the compile and run section again.
Intellisense C++ Standard
Intellisense is the feature that indicates compiler errors with red squiggly lines and suggests code completions. If the C++ standard is out-of-date, you’ll see squiggles where you shouldn’t.
First, you should already have the C/C++
extension installed (Instructions).
Next, open VS Code’s Command Palette with View > Command Palette
or with the keyboard shortcut ctrl + shift + P
on Windows or cmd + shift + P
on macOS. Search for and select C/C++: Edit Configurations (JSON)
. This will open the file c_cpp_properties.json
.
Modify the cStandard
and cppStandard
settings in c_cpp_properties.json
. Don’t change any other settings. Save the file.
{
"configurations": [
{
...
"cStandard": "c17",
"cppStandard": "c++17",
...
}
],
...
}
C/C++ extension alternatives
There are multiple options for C/C++ extensions.
Microsoft C/C++ extension provides debugging support and intellisense on Windows, Linux and macOS. At the time of this writing (January 2023) debug support has a bug on macOS.
CodeLLDB provides debugging support for those using the LLVM compiler. Apple’s compiler on macOS is based on LLVM.
clangd provides intellisense and requires the clangd
language server, which is related to the LLVM compiler. We do not recommend installing the clangd
extension with the Microsoft C/C++ extension because multiple intellisense providers can produce confusing results.
WSL lets us develop with Linux-based utilities like the g++
compiler.
Acknowledgments
Original document written by Andrew DeOrio awdeorio@umich.edu.
This document is licensed under a Creative Commons Attribution-NonCommercial 4.0 License. You’re free to copy and share this document, but not to sell it. You may not share source code provided with this document.