Skip to content

Dr. Nagender Aneja

Virginia Tech

  • Home
  • Publications
  • Patents
  • Teaching
  • Toggle search form

C++ Installation

C++ Installation and Setup Guide

This guide explains how to set up a basic C++ development environment on macOS, Windows, and Ubuntu. It also covers compiling a simple C++23 program, configuring Windows toolchains through WSL, MSYS2, and Visual Studio, using online compilers, and obtaining Catch2 for testing.

The slides begin with a simple “Hello 3514” program and demonstrate compiling it with g++ using the C++23 standard.

1. A First C++ Program

A minimal C++ program can be written as follows:

#include <iostream>

int main() {
    std::cout << "Hello 3514\n";
}

The program includes the standard input/output library and prints a message using std::cout.

To compile it with GCC using the C++23 standard:

g++ -std=c++23 main.cpp

On systems where the compiler produces the default executable a.out, run it with:

./a.out

This basic compile-and-run cycle is the foundation for the installation instructions that follow.

2. Installing C++ Tools on macOS

On macOS, the slides recommend installing Xcode from the Mac App Store. After installation, check whether the C++ compiler is available:

g++ --version

The slides also recommend installing CMake and Catch2 using Homebrew.

If Homebrew is not already installed, use the Homebrew installation command shown in the slides:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then install CMake:

brew install cmake

and Catch2:

brew install catch2

After installation, a C++ source file can be compiled using:

g++ -std=c++23 main.cpp

and run with:

./a.out

3. Installing C++ on Windows Using WSL

The first Windows option presented in the slides is Windows Subsystem for Linux (WSL).

WSL allows Linux commands and development tools to be used from Windows. The slides direct users to install WSL and then start it from the Windows command line using:

wsl

Once inside the Linux environment, install the standard development tools:

sudo apt install build-essential

Then compile the C++ program:

g++ -std=c++23 main.cpp

and run it:

./a.out

This approach provides a Linux-style C++ development environment while still working on a Windows machine.

4. Installing C++ on Ubuntu

On Ubuntu, update the package lists first:

sudo apt update

Then install the essential development tools:

sudo apt install build-essential

Compile the program with:

g++ -std=c++23 main.cpp

and run it using:

./a.out

The build-essential package provides the compiler and other commonly required tools for C and C++ development.

5. Installing C++ on Windows with MSYS2 and MinGW

A second Windows option in the slides is to use MSYS2 and MinGW.

MSYS2 provides tools and libraries for building, installing, and running native Windows software. The slides describe it as an easy-to-use environment for this purpose and direct users to download its installer from the MSYS2 website.

Install the Toolchain

After installing MSYS2, open its terminal and run:

pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain

The slides instruct users to accept the default options during installation and close the window after the installation completes.

The screenshot on page 7 shows this pacman command being entered directly in the MSYS2 terminal.

6. Configure the Windows Environment Variable

After installing the MSYS2 toolchain, Windows needs to know where to find g++.exe.

The slides instruct users to add the following directory to the Windows environment variables:

C:\msys64\ucrt64\bin

Before updating the environment variable, verify that g++.exe exists in that folder.

The screenshots on page 8 illustrate the process through Windows System Properties:

  1. Open Environment Variables.
  2. Locate the system Path.
  3. Edit the path.
  4. Add:
C:\msys64\ucrt64\bin

After saving the changes, the compiler should become accessible from a standard Windows command prompt.

7. Compile and Run with MSYS2/MinGW

The slides show compiling a program with:

g++ -std=c++23 main.cpp

The resulting Windows executable can then be run from the command line. The slides show:

run.exe

as the executable used in their example.

The key point is that once the MinGW compiler is installed and added to Path, g++ can be used directly from Windows.

8. Installing C++ with Visual Studio

The slides also show Microsoft Visual Studio as another Windows development option.

On page 10, the Visual Studio Installer screenshot highlights the workload:

Desktop development with C++

Selecting this workload installs the tools required for C++ development, including the Microsoft C++ compiler and related build tools.

The installation screen shown in the slides includes components such as C++ build tools, CMake support, debugging tools, and other development utilities.

9. Opening the Visual Studio Developer Command Prompt

After Visual Studio is installed, the slides show opening a developer shell through:

Tools → Command Line

The menu provides choices such as:

  • Terminal
  • Developer Command Prompt
  • Developer PowerShell

The screenshot on page 11 highlights these command-line options.

For the workflow demonstrated in the slides, use the Developer Command Prompt.

10. Compiling with Microsoft’s cl Compiler

Inside the Developer Command Prompt, move to the directory containing your C++ source file.

For example:

cd C:\Users\aneja\3514

Then compile the source file with Microsoft’s compiler:

cl /EHsc main.cpp

The slides explain the /EHsc option as enabling standard C++ exception handling.

They further break down the option as follows:

  • /EH controls C++ exception handling.
  • s assumes exceptions come from normal C++ throw statements.
  • c assumes C-style functions called from C++ do not throw C++ exceptions.

After compilation, run:

main.exe

11. Comparing the Windows Options

The slides effectively present three major ways to work with C++ on Windows.

WSL

Use WSL when you want a Linux-style development environment:

wsl
sudo apt install build-essential
g++ -std=c++23 main.cpp
./a.out

MSYS2 and MinGW

Use MSYS2 when you want a GCC-style compiler for native Windows development:

pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain

Then configure:

C:\msys64\ucrt64\bin

and compile with:

g++ -std=c++23 main.cpp

Visual Studio

Use Visual Studio when working with Microsoft’s C++ toolchain:

cl /EHsc main.cpp
main.exe

Each method provides a working C++ compiler but uses a different development environment and toolchain.

12. Using Online C++ Compilers

If installing a compiler locally is not convenient, the slides provide two online alternatives:

  • OnlineGDB
  • OneCompiler

These browser-based tools can be used to write, compile, and run C++ code without configuring a local toolchain.

Online compilers can be especially useful for quick experiments or small programming exercises.

13. Installing Catch2

The slides also introduce Catch2, a C++ testing framework.

They specifically mention obtaining the single-file catch.hpp version for macOS, Windows, or Ubuntu.

The slides point to the Catch2 version 2.13.7 release and also mention downloading the file from the course Canvas environment.

Once obtained, catch.hpp can be included in a C++ project to support unit testing.

14. Typical Development Workflow

After the compiler is installed, a basic workflow looks like this.

Create a source file named:

main.cpp

Write:

#include <iostream>

int main() {
    std::cout << "Hello 3514\n";
}

Compile using GCC or MinGW:

g++ -std=c++23 main.cpp

Then run:

./a.out

On Windows with Microsoft’s compiler:

cl /EHsc main.cpp

followed by:

main.exe

The exact commands differ by platform, but the overall process is the same:

Write source code
      |
      v
Compile the program
      |
      v
Create an executable
      |
      v
Run the executable

Conclusion

Setting up C++ primarily involves installing a compiler and ensuring that the operating system can locate it.

The slides present several supported environments:

  • macOS: Xcode, g++, Homebrew, CMake, and Catch2.
  • Ubuntu: build-essential and g++.
  • Windows with WSL: Linux development tools running through WSL.
  • Windows with MSYS2/MinGW: GCC-style native Windows development.
  • Windows with Visual Studio: Microsoft’s cl compiler and Developer Command Prompt.
  • Online environments: OnlineGDB and OneCompiler.

Once the toolchain is working, a C++23 source file can be compiled and executed from the command line, providing the basic environment needed for further C++ programming.

Copyright © 2026 Dr. Nagender Aneja.

Powered by PressBook WordPress theme