Skip to content

Installing a C++ Compiler — g++, Clang, MSVC, and CMake

DodaTech Updated 2026-06-28 7 min read

In this tutorial, you will learn about Installing a C++ Compiler. We cover key concepts, practical examples, and best practices to help you master this topic.

C++ source code must be compiled into machine code before execution, and this lesson shows exactly how to install and verify the compiler toolchain on every major operating system.

What You'll Learn

You will install the GNU C++ Compiler (g++), Clang, or Microsoft Visual C++ (MSVC) on your machine, set up CMake as a build system orchestrator, write and compile your first C++ program to verify everything works, and understand the differences between compiler flags and standards versions.

Why It Matters

Theory is useless without practice. Every C++ lesson from this point forward requires a working compiler. Installing a compiler is the first practical skill you need, and doing it correctly now prevents frustration later. Many beginners get stuck on linking errors, missing headers, or wrong standard versions — and all of these trace back to an incorrectly configured environment.

Learning Path

graph LR
    A["01: What is C++"] --> B["02: Installing a Compiler"]
    B --> C["03: Hello World"]
    C --> D["04: Variables & Types"]
    style A fill:#4a90d9,stroke:#2c5f8a,color:#fff
    style B fill:#4a90d9,stroke:#2c5f8a,color:#fff
    style C fill:#4a90d9,stroke:#2c5f8a,color:#fff
    style D fill:#4a90d9,stroke:#2c5f8a,color:#fff

GCC (g++) Installation

Linux (Ubuntu/Debian)

sudo apt update
sudo apt install g++ gdb make
g++ --version

Expected output (version may vary):

g++ (Ubuntu 13.2.0-23ubuntu4) 13.2.0

Linux (Fedora/RHEL)

sudo dnf install gcc-c++ gdb make
g++ --version

macOS

Install Xcode Command Line Tools which include Clang (not GCC, but compatible):

xcode-select --install
g++ --version

Note: On macOS, g++ is actually Clang with a GCC-compatible frontend.

Windows

Option A: MinGW-w64 (Minimalist GNU for Windows)

  1. Download from https://www.mingw-w64.org
  2. Add C:\mingw64\bin to your PATH
  3. Open Command Prompt and run g++ --version

Option B: Visual Studio Community (includes MSVC)

  1. Download from https://visualstudio.microsoft.com
  2. Select "Desktop development with C++" workload
  3. Open "Developer Command Prompt for VS" from Start Menu
  4. Run cl to verify MSVC compiler is available

Clang Installation

Clang is the default compiler on macOS and is available on all platforms.

Ubuntu/Debian

sudo apt install clang lldb
clang++ --version

Windows (with Visual Studio)

Clang is included with Visual Studio 2019 and later. Select the "C++ Clang tools for Windows" component during installation.

CMake Installation

CMake is a build system generator that creates Makefiles or Visual Studio project files from a CMakeLists.txt file. You will use it extensively in later lessons.

All Platforms

# Linux/macOS
sudo apt install cmake      # Ubuntu
brew install cmake          # macOS
# Windows: download from https://cmake.org/download/
cmake --version

Expected output (version may vary):

cmake version 3.28.3

Understanding Compiler Flags

When you compile C++ programs, you will pass flags that control the language standard, warnings, and optimization level:

g++ -std=c++20 -Wall -Wextra -Wpedantic -O2 main.cpp -o main
Flag Meaning
-std=c++20 Use the C++20 standard
-Wall Enable most common warnings
-Wextra Enable extra warnings
-Wpedantic Reject non-standard extensions
-O2 Optimize for speed (level 2)
-g Include debug symbols
-o output Name of the output executable

Always compile with warnings enabled. Warnings catch real bugs.

Compiler Differences

GCC, Clang, and MSVC implement the C++ standard with slight variations:

Feature GCC Clang MSVC
Default standard gnu++17 gnu++17 /std:c++14
C++20 modules Partial Full Partial
Conformance Excellent Excellent Good (improving)
Error messages Verbose Excellent Good
Compilation speed Moderate Fast Moderate

For learning, any of these compilers works. This series uses GCC with -std=c++17 as the baseline for examples.

Writing Your First Program

Create a file called test.cpp:

#include <iostream>

int main() {
    std::cout << "C++ compiler works!\n";
    return 0;
}

Compile and run:

g++ -std=c++17 -Wall test.cpp -o test
./test

Expected output:

C++ compiler works!

Using an Integrated Development Environment

You do not need an IDE to learn C++, but it can help. Popular options:

  • VS Code with the C++ extension pack (Microsoft, C/C++, CMake Tools)
  • CLion by JetBrains (commercial, excellent CMake support)
  • Visual Studio Community Edition (Windows-only, excellent debugger)
  • Qt Creator (cross-platform, good for GUI applications)

For this series, VS Code with the C/C++ extension is the recommended choice because it is free, cross-platform, and lightweight.

Common Mistakes

Mistake 1: Command Not Found

If your terminal says g++: command not found, you have not installed the compiler or it is not in your PATH. On Windows with MinGW, ensure C:\mingw64\bin is in your PATH environment variable.

Mistake 2: Wrong File Extension

C++ source files use .cpp (recommended), .cc, .cxx, or .C. Never use .c for C++ code — some compilers treat .c files as C code and reject C++ features.

Mistake 3: Mixing Compilers on One System

If you have both GCC and Clang installed, g++ and clang++ are separate commands. Running make may pick the wrong one. Use export CXX=g++ to set the C++ compiler explicitly.

Mistake 4: Forgetting the Standard Flag

Without -std=c++17 or -std=c++20, your compiler defaults to an older standard (gnu++17 on GCC 13, which is close to C++17 but with GNU extensions). Always specify the standard explicitly in learning projects.

Mistake 5: Ignoring Warnings

If your program compiles with warnings, treat each warning as an error. Add -Werror to your compiler flags to treat all warnings as errors. This catches subtle bugs early.

Mistake 6: Compiling C Code with a C++ Compiler

C code usually compiles under C++, but the reverse is not true. If you have a C library, compile it with gcc and link it with your C++ code using g++. Mixing gcc and g++ linking can cause undefined reference errors for C++ standard library functions.

Practice Questions

  1. Install g++ on your system and verify the version. What output do you get?
  2. Compile the test program above with -std=c++98. What happens? Why?
  3. What is the difference between -Wall and -Wextra? Try compiling with both flags versus just -Wall.
  4. Create a program that uses std::cout to print your name, compile it with -std=c++20, and run it.
  5. Research: What does the -pedantic flag do? Why would you use it?

Challenge

Set up a CMake project with a CMakeLists.txt file that builds a program printing "CMake works!". Use cmake -B build and cmake --build build to compile it.

FAQ

Which compiler is best for learning C++?

GCC (g++) is recommended for Linux, Clang for macOS, and MSVC for Windows. For cross-platform learning, GCC or Clang are preferred because they support more C++20/23 features earlier.

What is the difference between gcc and g++?

gcc is the GNU C Compiler; g++ is the GNU C++ Compiler. Both compile C++ if given a .cpp file, but g++ automatically links the C++ standard library. Use g++ for C++ code.

Why does my compiler not support C++20 modules yet?

Modules are a complex feature that requires coordination between the compiler and build system. GCC and MSVC have partial support; Clang has the best support as of 2025-2026. Use traditional headers for now.

Do I need a separate linker?

No. The g++ and clang++ commands call the linker automatically. You only need to run ld separately in rare cases or embedded development.

My compiled executable is very large. Why?

Static linking and debug symbols increase binary size. Use -s to strip symbols and -O2 for optimization. Debug builds are always larger than release builds.

Can I use C++ without CMake?

Yes. For simple programs, compiling directly with g++ is perfectly fine. CMake becomes essential when your project has multiple source files, external dependencies, or needs to build on multiple platforms.

Mini Project

Create a simple project structure with a build script:

cpp-playground/
  main.cpp
  build.sh

build.sh:

#!/bin/bash
g++ -std=c++20 -Wall -Wextra -Wpedantic -Werror main.cpp -o playground
./playground

Make it executable with chmod +x build.sh, then run ./build.sh. This script pattern will be the foundation for every lesson in this series.

What's Next

Your compiler is installed and verified. The next lesson writes the classic "Hello, World!" program and examines every piece of it in detail. You will learn about iostream, the std namespace, and what happens during each compilation stage.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro