C++ Get Started
To write C++ on your own computer you need two things: a text editor or IDE to write code in, and a compiler to turn that code into a program. On ExcelTAD you can skip the setup entirely — every example runs in the browser — but sooner or later you will want a local toolchain.
Installing a compiler
| Platform | Easiest option | Compiler command |
|---|---|---|
| Windows | Visual Studio Community (choose "Desktop development with C++") | cl |
| Windows (alternative) | MSYS2 + MinGW-w64 | g++ |
| macOS | xcode-select --install (Command Line Tools) |
clang++ |
| Linux | sudo apt install g++ (Debian/Ubuntu) |
g++ |
Popular editors and IDEs: Visual Studio, VS Code with the C/C++ extension, CLion, Code::Blocks and Qt Creator.
Compile and run from the terminal
Save the following code as hello.cpp:
#include <iostream>
int main() {
std::cout << "My first C++ program!\n";
}My first C++ program!
Then compile and run it:
$ g++ -std=c++20 -Wall -o hello hello.cpp
$ ./hello
My first C++ program!What the flags mean:
-std=c++20— use the C++20 language standard (the one used on this site).-Wall— turn on the most useful warnings. Always read your warnings!-o hello— name the output executablehello(on Windows it becomeshello.exe).
Tip: Make warnings part of your habit from day one. Add
-Wextratoo, and the compiler will catch many small mistakes before they turn into bugs.
What happens during compilation?
Building a C++ program happens in stages:
- Preprocessing — lines that start with
#(like#include) are handled. The contents of<iostream>are pasted into your file. - Compiling — each
.cppfile is translated into machine code, producing an object file. - Linking — object files and library code are joined into one executable.
If any stage fails, you get an error message and no program. Compile errors always mention a file name and line number — start reading from the first error, because later ones are often caused by it.
A program with a mistake
The code below forgets a semicolon. It is shown without a Run button because it does not compile:
#include <iostream>
int main() {
std::cout << "Oops" // missing ;
}g++ reports something like error: expected ';' before '}' token. Fix the line, compile again, and you are back in business.
Using the playground
Every runnable example on this site has three buttons: Run, Reset (restore the original code) and Copy. Try changing the program below so it prints your own name:
#include <iostream>
int main() {
std::cout << "Hello, my name is Ada.\n";
std::cout << "I am learning C++.\n";
}Hello, my name is Ada.
I am learning C++.
Test yourself
Which g++ flag selects the C++20 standard?
- -c++20
- --version=20
- -std=c++20
- -O20
Which build stage combines object files into one executable?
- Preprocessing
- Compiling
- Linking
- Interpreting
When a compile produces many errors, where should you start?
- The last error
- The first error
- Any random error
- The warnings only