Thuta Learning
C++
BasicProgrammingBeginner

Get Started: Set Up a C++ Compiler

C++Lesson 325 min

What you'll walk away with

  • Install g++, clang++ or MSVC and confirm your terminal can find it
  • Compile a .cpp file with -std=c++20 -Wall -Wextra and run the executable
  • Tell a 'command not found' problem apart from a compiler error
  • Read a compiler error by its file:line:column and fix the first error first

Explain it simply

Explain it simply

A C++ compiler is a translator. It reads your .cpp file, checks it against the rules of the language, and writes out a new file: an executable program in your computer's own machine code. Only then do you run that program. Unlike Python, there are two separate steps, and you repeat both every time you change the code.

So setting up C++ really means two jobs: installing a compiler, and making sure your terminal can find it. Online compilers are fine for the first few experiments, but a local setup lets you build real projects, work with files and see exactly the warnings your code produces.

Problem → Why → Solution

The problem: the command works in the tutorial but not for you

You copy a compile command from a tutorial and get one of two surprises. Either the terminal says it has never heard of g++, or the compiler runs and prints errors about code you copied exactly. The first means the compiler is missing or not on your PATH. The second often means your compiler is using an older C++ standard than the tutorial assumed.

terminal
$ g++ hello.cpp -o hello
bash: g++: command not found

$ g++ -std=c++17 hello.cpp -o hello
hello.cpp:6:23: error: 'format' is not a member of 'std'
hello.cpp:6:23: note: 'std::format' is only available from C++20 onwards

Why do these happen?

A shell looks for commands only in the folders listed in the PATH environment variable, so a compiler installed somewhere else stays invisible until its folder is added. And when you do not name a standard, each compiler picks its own default: GCC 16 uses C++20, but older GCC releases still in wide use default to C++17, which has no std::format. Always passing -std=c++20 removes the guesswork.

Core concept: source file, compiler, executable

Building a C++ program has three stages, and g++ runs all of them with one command. The preprocessor handles lines that start with #, pasting in headers such as <iostream>. The compiler checks syntax and types and turns your code into an object file of machine code. The linker joins that object file with the C++ standard library, which holds the real code behind std::cout and std::format, and writes the executable.

The command g++ -std=c++20 -Wall -Wextra hello.cpp -o hello says: compile hello.cpp as C++20, turn on the common warnings plus the extra ones, and name the output hello (hello.exe on Windows). Without -o, g++ names the result a.out on Linux and macOS and a.exe on Windows. You then run it as ./hello in a Unix shell or .\hello.exe in PowerShell. The ./ tells the shell to look in the current folder, which neither shell searches by default.

There are three major compilers. GCC provides g++ and is the standard on Linux. Clang provides clang++ and is the default on macOS, where the g++ command is usually just an alias for Clang. Microsoft's MSVC comes with Visual Studio and uses its own flag style, such as cl /std:c++20 /W4 /EHsc hello.cpp. All three implement the same ISO standard, so the code in this course compiles on any of them with C++20 enabled.

Warnings deserve a place in that command from day one. A warning means the code is legal but probably not what you meant, such as using a variable before giving it a value. With -Wall -Wextra the compiler reports many of these, and adding -Werror turns them into errors so they cannot be ignored.

Mental model: edit, build, run
  1. Edit hello.cpp in your editor
  2. g++ checks it and writes hello.exe
  3. Run ./hello and read the output
  4. Changed the code? Build again

Running an old executable after editing the source is the classic setup mistake: always rebuild first.

General pattern: compile and run commands

bash
# GCC or Clang (Linux, macOS, MinGW on Windows)
g++ -std=c++20 -Wall -Wextra file.cpp -o program
clang++ -std=c++20 -Wall -Wextra file.cpp -o program
./program

# MSVC, inside Developer PowerShell for Visual Studio
cl /std:c++20 /W4 /EHsc file.cpp
.\file.exe

GCC and Clang share their flags: -std= picks the language version, -Wall -Wextra turn on warnings and -o names the output. MSVC uses flags that start with / and names the output after the source file, with .exe.

Example: a hello.cpp that checks your toolchain

This program is a small setup test. If it builds, the compiler, the C++20 library and the linker all work, and the program itself reports which C++ standard it was compiled with. Build it with g++ -std=c++20 -Wall -Wextra hello.cpp -o hello and run it.

hello.cpp
#include <format>
#include <iostream>

int main() {
    std::cout << "Hello, C++!\n";
    std::cout << std::format("__cplusplus = {}\n", __cplusplus);
    std::cout << std::format("Standard year: {}\n", __cplusplus / 100);
    return 0;
}

Code breakdown

  1. Line 1#include <format>

    <format> is a C++20 header. In C++17 mode the lines that use std::format fail to compile. std::format first shipped in GCC 13 and Visual Studio 2019 version 16.10, so on older compilers the header itself may be missing.

  2. Line 5std::cout << "Hello, C++!\n";

    If this line appears, compiling, linking and running all worked.

  3. Line 6std::format("__cplusplus = {}\n", __cplusplus)

    __cplusplus is a macro the compiler predefines with the standard version as a number: with GCC, 201703 for C++17, 202002 for C++20 and 202302 for C++23. MSVC reports 199711 unless you add /Zc:__cplusplus.

  4. Line 7__cplusplus / 100

    Integer division drops the last two digits, so 202002 / 100 is 2020.

  5. Line 8return 0;

    Exit status 0 means success. In bash, type echo $? right after the program runs to see the 0.

You should see
Hello, C++!
__cplusplus = 202002
Standard year: 2020

Install a compiler on your system

SystemHow to installCheck that it works
Windows (GCC)Install MSYS2, run pacman -S mingw-w64-ucrt-x86_64-gcc in its UCRT64 terminal, then add C:\msys64\ucrt64\bin to PATHg++ --version in a new terminal
Windows (MSVC)Visual Studio Community with the "Desktop development with C++" workloadcl in Developer PowerShell for Visual Studio
macOSxcode-select --install (installs Apple Clang)clang++ --version
Ubuntu / Debiansudo apt install build-essentialg++ --version
Fedorasudo dnf install gcc-c++g++ --version

After installing, look at the version number that --version prints. std::format first shipped in GCC 13 and Visual Studio 2019 version 16.10, so if your Linux distribution's default compiler is older, you may need to install a newer one. For other toolchains, check the C++20 compiler support table on cppreference. The commands below are the everyday workflow.

terminal
g++ --version                                    # 1. is the compiler on PATH?
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello  # 2. build
./hello                                          # 3. run (macOS, Linux, Git Bash)
.\hello.exe                                      #    run in Windows PowerShell
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello && ./hello  # build, run only if it built

Reading your first compiler error

GCC and Clang errors share a shape: file:line:column, then error: or warning:, then the message. The lines after it show the offending source line with a ^ under the exact spot. The message below is what GCC 16 prints when std::cout is misspelled as std::cuot: line 4, column 10, with a suggested fix.

g++ output
hello.cpp: In function 'int main()':
hello.cpp:4:10: error: 'cuot' is not a member of 'std'; did you mean 'cout'?
    4 |     std::cuot << "Hello, C++!\n";
      |          ^~~~
      |          cout

Fix the first error first

One mistake can confuse the compiler into reporting many follow-on errors. Fix the topmost error, recompile, and watch how much shorter the list gets.

In real projects

How real projects build C++

Nobody types g++ commands by hand for a project with hundreds of files. A build system such as CMake reads a file that describes the project, generates the compiler commands and recompiles only the files that changed. The flags from this lesson are written once in CMakeLists.txt and apply consistently on Linux, macOS and Windows.

CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(hello LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(hello hello.cpp)

You build with cmake -S . -B build followed by cmake --build build. Teams then compile every push on continuous integration servers with GCC, Clang and MSVC, treating warnings as errors, because one compiler often catches what another misses. For quick comparisons of a short snippet across compilers, developers use Compiler Explorer (godbolt.org) in the browser.

Common mistakes

Common setup mistakes

1. Running an old build after a failed compile

When a compile fails, g++ writes no new executable, but the hello.exe from the previous build is still there. Run ./hello and you see the old code's output and conclude your fix did nothing. Write g++ ... && ./hello instead: && runs the program only if the build succeeded.

2. Compiling C++ with gcc instead of g++

gcc hello.cpp compiles the code as C++ because of the .cpp extension, but it does not link the C++ standard library. The result is linker errors such as "undefined reference to `std::cout'", ending with "ld returned 1 exit status". Use g++ or clang++ for C++.

3. Editing PATH and reusing an old terminal

A terminal copies PATH when it starts, so after you edit PATH in the Windows settings, windows that were already open still cannot find g++. Open a new terminal, or restart your editor if the terminal lives inside it, and check again with g++ --version.

Mini challenge

Mini challenge: prove your toolchain

Write setup_check.cpp, compile it once with -std=c++20 and once with -std=c++17, and compare the two outputs to see what the -std flag changes.

  • Store __cplusplus in a long variable named standard
  • Store whether standard >= 202002L in a bool named modern and print it as true/false with std::boolalpha
  • Print how many bytes an int uses with sizeof(int)
  • Do not use <format>, so that the C++17 build also succeeds
setup_check.cpp
#include <iostream>

int main() {
    // TODO: long standard = the value of __cplusplus
    // TODO: bool modern = is standard at least 202002L?

    // TODO: print a title line
    // TODO: print "Standard: <value>"
    // TODO: print "C++20 or newer: true/false" (hint: std::boolalpha)
    // TODO: print how many bytes an int uses (sizeof(int))
    return 0;
}

Expected: C++20 build: Standard: 202002 and C++20 or newer: true. C++17 build: Standard: 201703 and C++20 or newer: false. Both builds report that an int uses 4 bytes on mainstream desktop platforms.

Show hint

std::cout << std::boolalpha << modern; prints true/false instead of 1/0. Run g++ -std=c++20 -Wall -Wextra setup_check.cpp -o check && ./check, then repeat with -std=c++17.

Show solution
setup_check.cpp
#include <iostream>

int main() {
    long standard = __cplusplus;
    bool modern = standard >= 202002L;

    std::cout << "Setup check\n";
    std::cout << "Standard: " << standard << '\n';
    std::cout << std::boolalpha << "C++20 or newer: " << modern << '\n';
    std::cout << "An int uses " << sizeof(int) << " bytes\n";
    return 0;
}

Quick quiz 1 / 3

You edit hello.cpp, the compile fails with an error, and you run ./hello anyway. What happens?

Quick quiz 2 / 3

Which command compiles main.cpp as C++20 with warnings and names the program app?

Quick quiz 3 / 3

A program prints __cplusplus and shows 201703. What does that tell you?

What to remember

  • C++ must be compiled before it runs: preprocessor, compiler and linker produce the executable.
  • Use g++ -std=c++20 -Wall -Wextra file.cpp -o program as your standard command, or cl /std:c++20 /W4 /EHsc with MSVC.
  • "command not found" is a PATH problem; file:line:column: error: is a compiler problem.
  • Rebuild after every edit; g++ ... && ./program runs only after a successful build.

The Current ISO C++ Standard (C++23) — Standard C++ Foundation

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Get Started: Set Up a C++ Compiler | Thuta Learning