C++ Get Started

To get started with C++ you need a code editor and a C++ compiler. The compiler turns your .cpp source file into an executable program.

Installing a C++ Compiler

  • Windows: install Visual Studio Community with "Desktop development with C++", or MSYS2 with "pacman -S mingw-w64-ucrt-x86_64-gcc".
  • macOS: run "xcode-select --install" to get Clang (clang++).
  • Linux: run "sudo apt install g++" or "sudo dnf install gcc-c++".
  • Verify with "g++ --version" or "clang++ --version".

Compile and Run

Save this as main.cpp.

#include <iostream>

int main() {
    std::cout << "Compiled with C++20!\n";
}
Compiled with C++20!

Compile it with:

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

Run ./main (macOS/Linux) or main.exe (Windows). In main, return 0 is optional: reaching the end of main returns 0 automatically.

Choosing an Editor or IDE

Visual Studio (Windows), CLion and VS Code with the C/C++ extension are popular choices. Larger projects usually use CMake to describe how to build, which every major IDE understands.

Frequently asked questions

What is the difference between gcc and g++?

gcc is mainly the C compiler driver, while g++ compiles C++ and automatically links the C++ standard library. Use g++ for .cpp files.

How do I enable C++20?

Pass -std=c++20 to g++ or clang++, or /std:c++20 to MSVC. In CMake, set CMAKE_CXX_STANDARD to 20.

What file extension does C++ use?

Source files usually use .cpp (also .cc or .cxx), and header files use .h or .hpp.