Visual Studio Code Setup

Getting Started

To set up Visual Studio (VS) Code on your home computer to compile C++ programs, you need to download and install Visual Stuio Code proper, install and configure the GNU C++ compiler, and to install the VS Code C/C++ extension.

Installing Visual Studio Code

First, download the VS Code installer from [here](https://code.visualstudio.com/download). Once downloaded, click on the installer to start the intallation. Afte the installation completes, you can open the VS Code application.

Installing and Configuring the GNU C++ Compiler

VS Code may be configured with various compilers. We use GNU C++. The installation depends on the operating sytem.

Microsoft Windows

In Windows, we use MinGW installation package for GNU C++.

  1. Download MSYS2 distribution of MinGW windows installer from [this webpage] (https://www.msys2.org/).
  2. Run the installer. The installer window should look like this:
  3. When the installer prompts you to select the installation folder, use the default but note its path. Usually it is `C:\msys64\ucrt64\bin`
  4. Continue with the installation until you get to below window. Leave the "Run MSYS2 now." checkbox selected and click "Finish". >
  5. This opens a terminal window. Copy and paste the following command into this terminal window and press "enter": `pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain`.

    This command starts the installation of the MinGW package with GNU C++ compiler. Use default options for the installer in the terminal window.


  6. To finish the installation, add the path of the installed package (usually `c:\msys64`) to the PATH system variable. To do that, follow the below steps.

MacOS

  1. Press "cmd + space" to open spotlight search (or click the magnifying glass by the Wi-Fi icon in the top right corner) and type "terminal". Open the terminal application.
  2. Type "xcode-select --install" and press "Return" to install the command line developer tools.
  3. The GNU C++ compiler should now be available using the terminal:

    Type `g++` and press "Return". If the compiler is already installed, it produces an error complaning about missing input files:

    If you do not have it installed, MacOS may attempt to automatically install it: a window appears asking you to install the command line developer tools. Click "install" and enter your MacOS account password. Wait for the installation to finish.

Using Visual Studio Code

Open VS Code:

There are several buttons along the right side of the window.

Using the Compiler

Here is how to compile C++ source code using Visual Studio Code.

Installing C++ Extension

  1. In Visual Studio Code, open the extensions menu and search for C++.
  2. Install the extension that looks like this the image below. This extension should be authored by Microsoft (regardless of whether you are using a Mac or PC).

Hello World!

With the C++ extension installed, we should be able to use VS Code with the GNU compiler.

  1. Create a new folder and open it using the file explorer pane in the side bar.
  2. Create a new file called hello.cpp
  3. Copy the following code into the file:

    #include <iostream>

    int main() {

    std::cout << "Hello World!" << std::endl;

    }

  4. With the C++ file you want to compile open, click the play button in the upper right corner. You should see your program run in the terminal below!

Common Issues

If you instead are seeing an error, here are a couple of things you may need to adjust for VS Code to work.

  1. Check that VS Code is configured to use the GCC compiler.
  2. Check that the path used to access the compiler is the same as the installation path from earlier.