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++.
-
Download MSYS2 distribution of MinGW windows installer
from [this webpage] (https://www.msys2.org/).
-
Run the installer. The installer window should look like this:
-
When the installer prompts you to select the installation folder, use the default
but note its path. Usually it is `C:\msys64\ucrt64\bin`
-
Continue with the installation until you get to below window. Leave the "Run MSYS2 now." checkbox selected and click "Finish".
>
-
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.
- Press "enter" to accept the default packages
- When prompted whether or not to continue, press "y" and then "enter" to proceed with the installation
- Once the installation finishes you can close the window
-
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.
- open Windows Settings ("windows key"+s and search for "Settings")
-
find "Environment Variables" and select "System variables"; you
should see this window:
- in the bottom pane, select "Path" and then click the "Edit..." button.
- in the window that pops up, click "New" and enter the MinGW installation path that you noted earlier.
- once you did this, click "Ok" and exit the installer windows.
MacOS
-
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.
-
Type "xcode-select --install" and press "Return" to install the command line developer tools.
-
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.
- The top button opens a file browser to select a source file to be opened.
- The search button lets you search and replace text inside the opened files.
- The next "branch" button is used for version control.
- The icon with a play button provides options for debugging in folders which have a correctly configured launch.json file. For more information about debugging with VS Code click here
- - The icon with four squares allows you to install extensions which add extra features to visual studio code. Extensions can do a wide variety of things. Some extensions I use in my own setup include a PDF reader, live preview (for web development), and live preview of markdown files.
Using the Compiler
Here is how to compile C++ source code using Visual Studio Code.
Installing C++ Extension
- In Visual Studio Code, open the extensions menu and search for C++.
-
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.
- Create a new folder and open it using the file explorer pane in the side bar.
- Create a new file called hello.cpp
-
Copy the following code into the file:
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
}
- 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.
-
Check that VS Code is configured to use the GCC compiler.
- click the search bar and type `>debug`
-
Select the option to add a debug configuration
- Select the option for GCC
-
Check that the path used to access the compiler is the same as the installation path from earlier.
- In the file explorer open .vscode > tasks.json
-
Ensure that the file path being used for "command" is the same as the installation path for MSYS2.