The recommended development environment for C++ programming in CPTR 124 and CPTR 318 this semester is Microsoft's Visual Studio Code (VSCode). VSCode is available for free for Microsoft Windows, Apple macOS, and Linux. Many other fine C++ integrated development environments exist for these platforms, but each has its own peculiar way of configuring projects for editing, building, debugging, and executing C++ programs. The process of setting up a C++ project involving external libraries and other dependencies sometimes can become complicated, and it can be tedious to diagnose and correct configuration errors in a multitude of different development systems. Students using VSCode can be assured that they will receive timely support when things are not working as they should.
VSCode is a programming editor coupled with a lightweight project management system. VSCode itself does include the tools necessary for compiling and debugging C++. You must install the C++ development tools (compiler, linker, and debugger) separately and then configure VSCode to use these tools. Fortunately, installing the C++ tools and VSCode and configuring everything to work for our purposes is relatively easy on Windows, macOS, and Linux.
This document explains how to install and configure the necessary C++ development tools and VSCode on Windows, macOS, and Linux.
We will use a slightly enhanced version of Stephan T. Lavavej's distribution of the MinGW C++ compiler. Stephan's website has his current MinGW mix, but you should download and unzip MinGW-124.zip. This remix has an additional graphics library (GLUT) and a slightly modified console batch file to integrate better with VSCode. This MinGW package contains all the tools necessary for command-line development with C++ under Windows.
The following describes the necessary steps to install the VSCode enviroment for C++ development:
It is important that you use this exact path, or else the VSCode configuration files will not be able to find the C++ toolset.
C:\MinGW\vs_code_terminal.bat
.
You then should see a console window
appear. In the console window at the C:\MinGW>
prompt
type the command g++ -v
:
(The C:\MinGW>
part is prompt provided by the console, and
you type only the part that follows.)
The console window should respond by printing something similar to the following:
If instead you see
then something went wrong, and you should ask for help to fix it before continuing with the steps that follow.
This completes the installation of the MinGW C++ toolset.
Download the VSCode installer (https://code.visualstudio.com/), and install it as you would any other Windows application. It then should appear in your start menu as "Visual Studio Code." You can pin it to your large start menu or to your taskbar for quicker access. Launch VSCode to ensure that it installed correctly. VSCode should display a welcome screen similar to the one as shown here:
We are not quite ready to start C++ programming, but we almost are there.
This completes the installation of VSCode.
firstprogram.zip
file. This creates a folder named
FirstProgram
, and in that folder you will find two files and
and subfolder:
starter.cpp
contains the source code for a very
simple C++ program.
Makefile
is a configuration file that specifies how
to build the executable program from the source file.
.vscode
is a folder that contains several files
that allow VSCode to work properly with the MinGW C++ toolset.
The file starter.cpp
contains the following C++ program:
FirstProgram
folder you just downloaded and unzipped.
Direct VSCode to open this FirstProgram
folder.
starter.cpp
source file in a editor
by double-clicking on it.
Shift
and Ctrl
keys and
press the C
key. This brings up a console window.
make run
.
This command will attempt to build the executable program from the
source file, and, if it is successful, it will run the program.
In summary, the following three steps set up VSCode for C++ development under Microsoft Windows:
C:\
You need do these three set-up steps only once.
That is all there is to setting up VSCode for C++ development. Next we examine the typical workflow for creating a new C++ project.
After you have created your first C++ project, creating another one is very simple. To see how simple it is, let's create a new VSCode C++ project and supply our own C++ program.
You will follow steps similar to the following each time you want to write a C++ program using VSCode:
FirstProgram
), use Window Explorer to make a copy of
the FirstProgram
folder. For the sake of this exercise, name
it SecondProgram
.
SecondProgram
folder in VSCode.
At this point it is
an exact copy of the original project, FirstProgram
.
starter.cpp
in VSCode's Explorer pane.
Select "Rename" to change the file's name to secondprogram.cpp
.
(This step technically is not necessary. Since each C++ source file will be
in its own project folder, the source files could all have the same name.
For example, program.cpp
in the project folder
Project1
could be an entirely different program than in
the file program.cpp
in the Project2
project
folder.)
Makefile
is designed to work with the original project, and
we need to make a small change to adapt it to this new project. The
Makefile
is expecting a C++ source file named
starter.cpp
, but we just renamed it to be
secondprogram.cpp
.
Open Makefile
in the VSCode editor. Change the first line from
(Note that this step is unnecessary if you did not rename the source file in the previous step.)
SecondProgram
to behave differently from the one in our
FirstProgram
project.
Edit the secondprogram.cpp
file so that
it will display the following output when executed:
Shift
+ Ctrl
+ C
) and
type make run
. If the program does not build properly or does
run as expected, return to the VSCode editor and make changes to the source
code to fix build errors and/or correct the program's errant behavior.
It may take many edit-build-run cycles to achieve the finished product that
runs correctly.
In summary, the following three steps set up VSCode for C++ development under Microsoft Windows:
C:\
You need do these three set-up steps only once.
Once set up, the following three steps create a new C++ project in VSCode:
Makefile
to reflect the
C++ source file's new name (not necessary if you skipped Step 2)
You perform these four steps every time you wish to write a new program using VSCode.
To build and run a C++ program under VSCode, bring up a VSCode console window
(press Shift
+ Ctrl
+ C
) and
type make run
. Repeat the edit-build-run sequence until the program
behaves correctly.