Getting Started

Setting up Visual Studio Code, your coding environment, and becoming familiar with the tools for debugging and executing your first C++ program.

In order to begin programming, we need a programming (or development) environment. Text editors, such as Microsoft Word, allows us to write/edit text. The text editor that we will be using in class to write/edit code is Visual Studio Code, or “VS Code” for short.

  1. Install Visual Studio Code
  2. Enable C++
  3. Install C++ Compiler
  4. The Hello World Program
  5. Additional Resources

Alternative: If for some reason you are not comfortable with any of these tools, create an account on REPL.it https://repl.it/.

Install VS Code

Note: if you’re a Mac OSX user, jump to Installing XCode.

The following video tutorial will walk you through setting up Visual Studio Code (https://code.visualstudio.com/) and gives a general overview of its features.

Highlights

  • 0:05 - 0:30 Download and install VS Code with the default settings
  • 0:35 - 1:17 See an overview of the user interface.
  • 1:22 - 1:31 Install support for your programming language(s).
  • 1:47 - 2:05 Customize your editor with themes.

Install the C++ Extension

If this is your first time using VS Code, your editor can only understand JavaScript code. To enable C++ in VS Code, click on Extensions, the last icon on the sidebar.

In the search bar, type C++ to find the official Microsoft C/C++ extension. Press Install, then Reload

With the C/C++ extension installed, create a folder (I chose to create one on my Desktop) named VSCode. Then, create a subfolder to house only your C++ code (~/Desktop/VSCode/cpp/). Visual Studio Code will automatically place various settings files into a .vscode subfolder.

Now, we want this folder (~/Desktop/VSCode/cpp/) to be our designated workspace for our C++ code. To configure this in VS Code, click on Explorer, the first icon on the sidebar.

Click Open Folder and navigate to the folder you’ve just created (~/Desktop/VSCode/cpp/). Click Select Folder to confirm.

Install & Configure a C++ Compiler

You may already have a C++ compiler installed. You can test this by opening Terminal (Mac) or cmd.exe (Windows) and entering g++. If you get a warning along the lines of ... error: no input files, then your system already has a C++ compiler. Otherwise, if you get an error about the command not being found, then the C++ compiler may not be installed.

At this moment, if you’ve been following this tutorial, we only have a text editor for writing C++ code. This means that much of what we can now do in VS Code is write/edit text as we do in MS Word. A compiler is useful because it translates our human-readable code into machine code or a lower-level language so that now the computer can understand and execute our program.

Windows

The following video tutorial shows how to install the MinGW compiler for Windows 10.

Mac OSX

Instead of Microsoft’s Visual Studio Code and MinGW (for which the “W” stands for “Windows”), download XCode from the app store to compile C++ programs. You can still download VS Code (which is compatible with Mac OSX) because this is what we’ll be using in class. Unlike VS Code, XCode already comes with a C++ compiler that is compatible with Mac OS. If you wish to avoid the overlap, check out the options provided here: https://github.com/kennethreitz/osx-gcc-installer#option-1-downloading-pre-built-binaries

In the menu, open File, hover over New, and select Project.

Choose the Command Line Tool template

Choose project type as C++

Create project and click on main.cpp to make/edit your project. You can run the program under the Product tab > Run.

hello.cpp

To create our first C++ program, navigate to the Explorer icon on the sidebar, under our workspace CPP, click New File:

Type hello.cpp and press enter. In the created file, paste and save the following code. This is a basic “Hello World” program that does only one thing: print Hello World.

// Preprocessor directive that includes header iostream
#include <iostream>

// Start of your program: function block main()
int main() {
	/* Write to the screen */
	std::cout << "Hello World" << std::endl;

	// Return a value to the OS
	return 0;
}

Note: The purpose of this guide is to help you configure a text editor and compiler for executing C++ programs. The anatomy of this C++ program and what the g++ command means will be discussed in later notes.

In the Terminal (Mac) or the cmd.exe (Windows), navigate to the workspace previously created (~/Desktop/VSCode/cpp/) using the command cd (which means change directory).

Type the command g++ -o hello hello.cpp to compile your program. This creates an executable file (~/Desktop/VSCode/cpp/hello.exe). Clicking on the file will prompt its execution, which may look like a single flash of a black box on the screen. This is because the program has executed and was closed immediately upon completion.

To view the results of your program, return to the command line and enter hello.exe. You should see the words “Hello World”. If not, you should see an error which may result in a typo (formally called a syntax error) in the source code.

Additional Resources


© 2020. Some rights reserved.