Command Lines and Compilers

Learn how to use the command line and install a C++ compiler (GCC, Clang, or MSVC)

Greg Filak
Published

The goal of this chapter is to get a C++ development environment fully set up and ready for the rest of the course. We'll learn some command-line basics and set up a compiler.

The Command Line

Before we install a compiler, we need to introduce the primary environment where we'll be working: the command-line interface (CLI), also known as the terminal or console.

If you've spent most of your time in graphical user interfaces (GUIs), clicking on icons and menus, the terminal can seem intimidating. It's just a blank screen with a blinking cursor.

But it's also the most powerful and direct way to interact with your computer's operating system and, more importantly, with the developer tools we're about to install.

Opening Your Terminal

First, you need to know how to open a terminal on your system.

  • On Windows: You have several options. You can search for Command Prompt (cmd.exe) or the more modern PowerShell. If you install Visual Studio, it provides a special Developer Command Prompt which is pre-configured with the environment variables needed to use the MSVC compiler. This is often the most convenient choice. For this course, any of them will work.
  • On macOS: The terminal application is simply called Terminal. You can find it in your Applications/Utilities folder, or just press Cmd + Space to open Spotlight and type "Terminal".
  • On Linux: This is the native environment for most developers. Your distribution will have an application called Terminal, Konsole, or something similar. The keyboard shortcut Ctrl + Alt + T will often launch it on Ubuntu and related distributions.

Commands for Navigation

You don't need to be a terminal expert, but you do need to know how to navigate the file system.

This command tells you where you currently are in the file system. It's the "You are here" map.

pwd
/home/user/documents

In Windows' command prompt, the current location is always shown, so there is no equivalent command.

List: ls

This command lists all the files and folders in your current directory. It's how you see what's around you. On the Windows Command Prompt, the equivalent command is dir.

ls
ProjectA/
  notes.txt
  images/

Change Directory: cd

This is how you move around. You can move into a sub-folder or go back up to a parent folder.

cd ProjectA

You can check where you are again using pwd:

pwd
/home/user/documents/ProjectA

To navigate up to the parent directory, we can use ..

cd ..
pwd
/home/user/documents

We'll introduce more commands later in the chapter, and the effect that these commands will have typically depends on where we are in the file system.

With these three commands - pwd, ls, and cd - you have everything you need to navigate around your file system and project folders to ensure your running future commands in the correct location.

Using Tab Completion

The single most useful feature of any modern terminal is tab completion. When you're typing a file or directory name, just type the first few letters and press the tab key. The terminal will automatically complete the rest of the name for you. If there are multiple possibilities, pressing tab twice will show you all of them.

This saves a huge amount of time and prevents typos.

Meet the Compilers

There are several C++ compilers available, but three stand out as the most widely used in the industry - GCC, Clang, and MSVC. For this course, it's not particularly important which one you choose - any of them will work.

Later in the chapter, we'll set up CMake, which is the most popular tool for managing C++ projects. It handles the interaction with the compiler for us, and is compatible with all three options:

GCC (GNU Compiler Collection)

GCC is one of the oldest and most established compilers. It originated as part of the GNU Project and is the standard compiler on most Linux systems. When you see the g++ command, you're using GCC's C++ compiler. It's known for its broad platform support and adherence to standards.

Clang

Clang is a newer compiler front-end, developed as part of the LLVM project. It was designed to be faster and use less memory than GCC, but its most popular feature is its incredibly clear and helpful error messages.

Clang is the default compiler on macOS (via Apple's toolchain) and is widely used on other platforms, including Windows and Linux. Its command-line interface, clang++, is designed to be a drop-in replacement for g++.

MSVC (Microsoft Visual C++)

If you're a Windows developer, you're likely familiar with Visual Studio. The compiler that powers it is MSVC (cl.exe). It's a highly optimizing compiler with excellent integration into the Windows ecosystem.

Setting Up a Build Environment on Windows

Windows is the most complex platform to set up. You have several choices.

This is the "batteries-included" approach and the easiest way to get started.

  1. Download Visual Studio Installer: Go to the Visual Studio download page and get the "Community" edition, which is free for individuals and open-source projects.
  2. Run the Installer: When the installer launches, it will show a list of "Workloads."
  3. Select the C++ Workload: Check the box for "Desktop development with C++". This single click will install everything you need: the MSVC compiler, the Windows SDK (which contains system headers and libraries), and a recent version of CMake.
  4. Install: Click the install button and wait for the process to complete.

Once installed, you can access the compiler by opening the Developer Command Prompt for VS, which you can find in your Start Menu.

Option 2: MSYS2 (A GNU/Linux-like Environment)

If you prefer a command-line experience similar to Linux, using GCC on Windows is a great option. The best way to do this is with MSYS2.

Install MSYS2: Go to the MSYS2 website and follow their installation instructions. This will install a package manager called pacman, and some terminals we can use. We'll use the MSYS2 UCRT64 terminal in this example

Update MSYS2: Open the MSYS2 UCRT64 terminal and run the following command to make sure everything is up to date:

pacman -Syu

You may need to close and reopen the terminal and run it again to complete all updates.

Install the Toolchain: Now, use pacman to install the MinGW-w64 toolchain, which includes GCC (g++), the linker, make, and other essential tools.

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

This command is likely to install a lot of packages, as there's often a complex web of dependencies involved (that is, packages that require other packages to be installed)

That's it! We'll confirm everything is working correctly in the "Verifying Your Setup" section below.

Setting Up a Build Environment on macOS

macOS makes this process very easy. The operating system ships with stubs for common developer tools, and trying to use them will prompt an installation.

Open the Terminal: You can find it in Applications/Utilities.

Run the Installer Command: Type the following command and press Enter.

xcode-select --install

Confirm Installation: A dialog box will appear asking if you want to install the command line developer tools. Click "Install" and agree to the terms.

This will download and install Apple's Command Line Tools package, which includes clang, clang++, make, and other necessary utilities. It's the official, Apple-supported way to get a C++ compiler on macOS.

Setting Up a Build Environment on Linux (Debian/Ubuntu)

Linux is a developer's paradise. Getting a C++ toolchain is typically a single command away. The instructions here are for Debian-based distributions like Ubuntu, but the process is similar on other distros. This might involve using dnf on Fedora/CentOS or pacman on Arch.

Update Your Package Lists: It's always good practice to update your package manager's index first.

sudo apt update

Install build-essential: This is a special meta-package that installs everything you need for basic compilation, including gcc, g++, and make.

sudo apt install build-essential g++

That's it! You now have a complete GCC toolchain ready to go. If you prefer to use Clang, you can install it just as easily:

sudo apt install clang

Verifying Your Setup

Once you've completed the installation for your platform, it's a good idea to verify that the compiler is correctly installed and accessible from your terminal. Open a new terminal window and try one of the following commands, based on the compiler you installed:

To check for GCC:

g++ --version
gcc.exe (Rev6, Built by MSYS2 project) 15.1.0
Copyright (C) 2025 Free Software Foundation, Inc.

To check for Clang:

clang++ --version
Apple clang version 15.0.0 (clang-1500.3.9.4)
Target: arm64-apple-darwin24.5.0

If you're on Windows using the Visual Studio Developer Command Prompt, check for MSVC:

cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.43.34810 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

If you see output that includes a version number, congratulations! Your compiler is installed and ready.

Summary

In this lesson, we laid the essential groundwork for the rest of the course.

We started by getting comfortable with the command-line interface, learning the basic navigation commands (pwd, ls, and cd).

Then, we met the three major C++ compilers: GCC, Clang, and MSVC. We walked through the specific setup instructions for each major operating system:

  • Windows: Using the all-in-one Visual Studio installer or setting up a Linux-like environment with MSYS2.
  • macOS: A simple one-liner, xcode-select --install, to get Apple's command-line tools.
  • Linux: A quick apt install for Debian/Ubuntu users to get the build-essential package.

Finally, we verified our installation by checking the compiler's version from the terminal, ensuring that our system is ready for the next steps.

Next Lesson
Lesson 13 of 25

Version Control with Git and Submodules

Learn the basics of Git for version control, including initializing repositories, making commits, and managing project dependencies with submodules.

Have a question about this lesson?
Answers are generated by AI models and may not be accurate