Visual Studio Code Debugging

Setting up VS Code

Instructions for setting up Visual Studio Code to compile C++ are available [here](vscodelocal.html). Upon completing this tutorial, you should be able to run the debugger.

Debugging with VS Code

Set breakpoints

To determine how the debugger steps through your program, you can set breakpoints. In VS Code, breakpoints are set by clicking the space between the line numbers and the right pane. A red dot will appear on the line, and the debugger will stop execution before the line is ran.

Run the debugger

With breakpoints set, the debugger can be ran by clicking the dropdown next to the play button (the same one used to compile) and selecting debug from the options that appear.

The debugger runs up to the first breakpoint, and you are presented with a screen similar to this:

Step through your program

A series of buttons appear at the top of the window when the debugger is running. These buttons are used to control how the debugger steps through the program. The buttons function as follows (from left to right):

Compiling and Debugging with Multiple Files

notes

If your project has multiple files, you can configure the `tasks.json` file to compile all of the C++ files you currently have open. Do this by opening .vscode -> tasks.json and replacing `${file}` with `${workspaceFolder}/*.cpp` under the `args` entry.

With this configuration change made, go back to the file with the main part of your program and run the compiler. You should be able to step through functions included in other files when breakpoint are set accordingly.

Debugging in MacOS

MacOS does not support standard input when debugging using the VS Code console. In order to debug projects using MacOS, the XCode debugger can be used.

Using XCode

  1. Install XCode if not already instealled
  2. Create a new project, select "Command Line Tool" under "macOS"
  3. Name the project and select C++ as the language
  4. Save the project within your subversion repository
  5. Delete the template files and move anything you have already worked on into the project directory created by XCode
  6. Click the play button above the file browser when you are ready to build your project
  7. Breakpoints work similarly to VS Code and can be set/unset by clicking the margin between the line number and the file browser