C++ · Module 1 · Lesson 21 min read

Setting Up Your C++ Environment

The three major compilers

CompilerCommandPlatform
GCC (GNU)g++Linux, Windows (MinGW/MSYS2), macOS
Clang (LLVM)clang++macOS (default with Xcode), Linux, Windows
MSVC (Microsoft)clWindows (Visual Studio)

All three support C++20 well. Check your version: g++ --version.

Installing

  • Windows: install Visual Studio Community (choose "Desktop development with C++"), or MSYS2 for g++
  • macOS: run xcode-select --install (gives clang++)
  • Linux: sudo apt install g++ (Ubuntu/Debian)
  • Android phone: apps like Cxxdroid let you practise on the go

Editors and IDEs

  • Visual Studio (Windows): full IDE with a great debugger
  • VS Code + the C/C++ extension: light and popular
  • CLion (JetBrains): powerful paid IDE, free for students
  • Code::Blocks / Dev-C++: common in schools and colleges
  • Online: Compiler Explorer (godbolt.org), OnlineGDB, Programiz — no install needed

Compiling from the terminal

ini
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello
./hello            (Windows: hello.exe)
FlagMeaning
-std=c++20use the C++20 standard
-Wall -Wextraturn on useful warnings ✅ always use them
-o helloname of the output program
-ginclude debug information
-O2optimize for speed

File extensions

Source: .cpp (also .cc, .cxx) · Headers: .h or .hpp

Tip

Treat warnings as errors you haven't found yet. Fix every warning.

Example

C++
#include <iostream>

// Check which C++ standard your compiler is using.
// __cplusplus is a number set by the compiler.
int main() {
    std::cout << "__cplusplus = " << __cplusplus << '\n';

    if (__cplusplus >= 202002L)
        std::cout << "You are using C++20 or newer\n";
    else if (__cplusplus >= 201703L)
        std::cout << "You are using C++17\n";
    else
        std::cout << "Older standard: add -std=c++20 when compiling\n";
    return 0;
}

/* Output:
__cplusplus = 202002
You are using C++20 or newer
*/