C++ Tutorial · C++ Basics · Lesson 2 of 21

C++ Get Started

Install a compiler, compile and run a program, and use CMake.

Install a compiler

  • Linux: sudo apt install g++ (or clang).
  • macOS: xcode-select --install (installs clang).
  • Windows: Visual Studio Community ("Desktop development with C++"), or MSYS2 for g++.

Compile and run

C++ is compiled to a native executable:

g++ -std=c++20 -Wall -Wextra -O2 main.cpp -o main
./main

-Wall -Wextra turn on warnings — always read them.

Example 1
#include <iostream>

int main() {
    std::cout << "C++ standard: " << __cplusplus << '\n';
    std::cout << "Compiled and running!\n";
}

Output

C++ standard: 202002
Compiled and running!

Run this on your computer — see “Get Started” for setup.

Build systems

Real projects use CMake to describe how to build on every platform:

cmake_minimum_required(VERSION 3.20)
project(hello CXX)
set(CMAKE_CXX_STANDARD 20)
add_executable(hello main.cpp)

Test yourself

Which flag selects the C++20 standard in g++?

  1. --cpp20
  2. -std=c++20
  3. -c++20
  4. -version=20
Show answer

Answer: -std=c++20