EECS 280 Tutorials

Setting up Visual Studio

Visual Studio is a feature-rich integrated development environment (IDE) that runs on Windows.

If you already have Visual Studio installed, skip to the Create a project section.

Prerequisites

Visual Studio runs on Windows only. There are no other prerequisites for Visual Studio.

This tutorial recommends command line tools. To install CLI tools, follow the Windows command line tools tutorial.

Next, we recommend our Command line interface (CLI) tutorial.

Pitfall: Make sure you have installed CLI tools for Windows before continuing.

Install

Install Visual Studio Community edition from Microsoft’s website.

Select “Desktop Development with C++” and make sure that the “Windows 10 SDK”, “Windows 11 SDK”, and “C++ AddressSanitizer” are selected. Click “Continue”. Reboot your computer when the installer prompts you.

Start Visual Studio. The first time, you’ll be asked about a few preferences.

The screenshots in the tutorial were created with Visual Studio Community 2022. Your version might be different.

Create a project

A Visual Studio project contains the files and information to build your software. In EECS 280, you’ll eventually create one Visual Studio project for each EECS 280 project.

Start Visual Studio. Create a new project. You can also use “File” > “New” > “Project”.

Select “Console App”. Click “Next”.

Set a project name, we’ll call our example project p1-stats. Pick a location where you’ll store your projects. Check “Place solution and project in the same directory. We recommend this in EECS 280 so that everyone has a one-level structure (p1-stats/main.cpp) instead of a two-level structure (p1-stats/p1-stats/main.cpp). Click “Next”.

Pitfall: Avoid paths that contain spaces. Spaces cause problems with some command line tools.

Bad Example Good Example
EECS 280/ eecs280/
Project 1 Stats/ p1-stats/

You can see the files created by Visual Studio in the File Explorer. Right-click p1-stats, select “Show in Finder”.

You can also see the files created by Visual Studio from the WSL command line. Notice that Visual Studio created .sln, and vcxproj files.

$ tree
.
├── p1-stats.cpp
├── p1-stats.sln
├── p1-stats.vcxproj
├── p1-stats.vcxproj.filters
└── p1-stats.vcxproj.user

Rename main file

Rename the default p1-stats.cpp to main.cpp. Your main filename may be different. Use Visual Studio to rename a file, not the command line or File Explorer.

Right-click p1-stats.cpp and select “Rename”. Change the file name.

Add new files

Visual Studio created main.cpp by default. Skip this subsection your first time through the tutorial. You can come back to it.

Open your project folder by selecting File > Open > Project/Solution, navigate to your project directory (p1-stats in this example) and open p1-stats.sln. An alternative is to double-click p1-stats.sln in the File Explorer.

Right-click “Source Files”, then select “Add” > “New Item”.

Select “Visual C++” and “C++ File”. Name your file, we’ll use stats.cpp for this example. Click “Add”.

You should see your new file under “Source Files”.

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
p1-stats.sln
...

Pro-tip: Copy/paste instructions for WSL.

We’ll use the WSL (Ubuntu) terminal to download, unpack, and move 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 the src subdirectory.

$ tree
.
├── Makefile
├── cats.csv
├── cats.out.correct
├── main.cpp
├── p1-stats.sln
├── p1-stats.vcxproj
├── p1-stats.vcxproj.filters
├── p1-stats.vcxproj.user
├── p1_library.cpp
├── p1_library.hpp
├── stats.hpp
├── stats_public_tests.cpp
├── stats_tests.cpp.starter
└── two_sample.cpp.starter

Right-click “Source Files”, then select “Add” > “Existing Item”.

Navigate to your project directory. Select your files by holding Control and clicking each one. Your specific files may not match the image below. Do not select any .sln or .vcxproj files. Click “Add”.

You will now see your files in the sidebar in the sidebar.

Rename files

If you need to rename any files, use Visual Studio, not the command line or File Explorer. 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.

Compile and Run

A Visual Studio Build compiles one executable.

Exclude files from build

We need to avoid multiple main() functions in the same build. Exclude files not needed to compile the main program. This often means excluding unit tests from the build.

In this example from EECS 280 Project 1, we need to exclude our unit tests from the build because they each contain a main() function.

Right-click a file in the solution explorer (sidebar) and select “Properties”. Set “Excluded From Build” to “Yes”.

You can see a red symbol next to each excluded file.

“Build” -> “Build Solution”. See that the build output was successful.

Run

Click the “Local Windows Debugger” button. You should see your program run.

Sanitizers

Visual Studio provides an address sanitizer with bounds checking automatically with every debug build. You don’t need to do anything different!

Pitfall: When Visual Studio says “Press any key to continue”, press a key, do not close the window by clicking the X. Some memory checks run after you press any key.

Command-Line Arguments and Options

Skip this subsection your first time through the tutorial. You can come back to it.

Inputs to a program may be provided when it is initially run via command-line arguments or options. Here’s an example from EECS 280 Project 1:

$ ./two_sample.exe HCMST_ver_3.04.tsv q24_met_online 1 0 ppage

The arguments above specify the name of a data file, coulumns, and filter values for the program to use.

Right-click the project in the Solution Explorer (p1-stats in this example). Select “Properties”.

Edit “Command Arguments” and click “OK”.

Input redirection

Skip this subsection for EECS 280 project 1.

If you’re unfamiliar with input redirection, first read the CLI tutorial section on input redirection.

Without input redirection, here’s how to type input in the Visual Studio command line. Notice that when we run, a window pops up that accepts user input.

To configure input redirection, right-click the project in the Solution Explorer (p1-stats in this example). Select “Properties”.

Edit “Command Arguments” and click “OK”.

Pitfall: With input redirection configured, Visual Studio will automatically close the terminal window, and you’ll miss seeing the output.

Set a breakpoint on the last line of main to keep the output window open.

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

Run the debugger. The program pauses at the breakpoint. The indicator highlights the next line of code to be run.

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 Autos pane.

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

To reset Visual Studio project settings and starter files, first quit Visual Studio. Make a backup copy of your files, and then delete your project directory. Your project directory might be different.

/Users/awdeorio/src/eecs280
$ cp -a p1-stats p1-stats.bak  # Backup
$ rm -rf p1-stats              # Delete

Visual Studio has a lot of settings. You can reset the entire user interface to the default settings by selecting “Tools” -> “Import” and Export Settings” -> “Reset all settings”. This is optional.

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.