Hello World (first programs) · Beginner · 14 min
Hello World in C++: std::println, g++ and a CMake build
By Padding · Updated 18 Nov 2026
Debian 13 (gcc:16 image) · GCC 16.2 · CMake 3.31
You will compile and run a C++23 Hello World with g++, read a name from the keyboard into a std::string, and build the same program with CMake the way real C++ projects are built.
On this page
C++ is a compiled language. You write source code in a text file, a compiler turns it into a native program for your processor, and you run that program directly. Nothing interprets your code while it runs, which is a large part of why C++ is still the language behind game engines, web browsers, trading systems, databases and a lot of embedded software.
Two things about C++ trip up newcomers more than the syntax does. First, the language changes every three years (C++17, C++20, C++23), and the compiler only lets you use newer features if you ask for that standard. Second, a real project is rarely compiled with one hand-typed command; it is described in a build file, most often for CMake.
In this tutorial you install the GNU C++ compiler, g++, and CMake. You write Hello World twice, once with the classic std::cout and once with C++23's std::println, read a name from the keyboard, learn to read a long compiler error from the top, and finish with a small CMake build. Every command was written for GCC 16.2; any g++ from version 14 onwards supports everything used here.
Step 1 · Install g++ and CMake
The build-essential package brings in g++, the C compiler, make and the standard library headers. cmake is the build tool you use in the last step.
sudo apt update
sudo apt install -y build-essential cmake
g++ --version
cmake --versionIf you are on prepaid data, leave out -y the first time: apt then prints how much it will download and asks before it starts.
Check it worked
g++ --version prints a first line such as g++ (GCC) 16.2.0; distribution builds show their own name in the brackets. The version number after the brackets must be 14 or higher for std::println to work. cmake --version prints cmake version 3. or 4. followed by more numbers.
Troubleshooting
If your distribution's g++ is older than 14, std::println will not be found in step 3. Upgrade the distribution, or install a newer compiler package (for example g++-14) and use that name in place of g++.
Step 2 · Write and run Hello World with std::cout
Make a folder for this tutorial and write the first program into it:
mkdir -p ~/hello-cpp-demo
cd ~/hello-cpp-demo
cat > hello.cpp <<'CPP'
#include <iostream>
int main() {
std::cout << "Hello, world!\n";
return 0;
}
CPPLine by line:
#include <iostream>pastes in the declarations for C++'s input and output streams before compiling. Lines starting with#are handled by the preprocessor, a text-substitution pass that runs before the compiler proper. A header only declares things; the code behindstd::coutlives in the C++ standard library (libstdc++ with g++), which g++ links in for you.int main()is where every C++ program starts. Theintsays it returns a whole number to the operating system.std::coutis the standard output stream, your terminal.std::means it belongs to the standard library's namespace, a named group that keeps its names apart from yours.<<sends the text on its right into the stream on its left.\nis a newline.return 0;tells the operating system the program succeeded. Any other number means failure. Every statement ends with;.
Compile it, then run the program the compiler produced:
g++ -std=c++23 -Wall -Wextra hello.cpp -o hello
./hello-std=c++23 picks the language standard, -Wall -Wextra turn on the warnings worth seeing, and -o hello names the output file. Without -o, g++ writes a file called a.out. The ./ tells the shell to run the program in the current folder rather than search your path for one.
Check it worked
ls shows a new file called hello next to hello.cpp, and ./hello prints Hello, world!. That file is a native program: it runs without g++, and you could copy it to another machine with the same operating system and processor type.
Step 3 · Use C++23 std::println and see why the standard flag matters
C++23 added std::println, which prints a line with Python-like {} placeholders and adds the newline for you. Write a second version:
cat > hello_print.cpp <<'CPP'
#include <print>
int main() {
std::println("Hello, world!");
std::println("This program was built with C++ standard {}.", __cplusplus);
}
CPP
g++ -std=c++23 -Wall -Wextra hello_print.cpp -o hello_print
./hello_printWhat is new:
#include <print>is the C++23 header forstd::printandstd::println.- Each
{}in the first argument is replaced by the next argument. Here it is__cplusplus, a number the compiler defines to say which standard it is compiling for. - There is no
return 0;.mainis the one function allowed to leave it out; reaching its end means success.
Now compile the same file for C++20 to see what the flag does:
g++ -std=c++20 hello_print.cpp -o hello_print20 2> std20-errors.txt || echo "Compile failed, as expected"
grep -m1 "error:" std20-errors.txtGCC 16 compiles for C++20 (with GNU extensions) when you give no flag at all, and older versions default to C++17, so leaving the flag out gives you the same failure. Always write -std=c++23, or set it in the build file as you will in the last step.
Check it worked
./hello_print prints Hello, world! and then This program was built with C++ standard 2023... followed by two more digits. The C++20 compile prints Compile failed, as expected, and its first error says something like 'println' is not a member of 'std': the header exists, but its contents are switched off below C++23.
Step 4 · Read a name with std::getline
A program that only prints is half a program. This one asks for your name and greets you:
cat > greet.cpp <<'CPP'
#include <iostream>
#include <print>
#include <string>
int main() {
std::print("What is your name? ");
std::string name;
std::getline(std::cin, name);
if (name.empty()) {
name = "stranger";
}
std::println("Hello, {}! Your name has {} characters.", name, name.size());
}
CPP
g++ -std=c++23 -Wall -Wextra greet.cpp -o greet
printf 'Ada Lovelace\n' | ./greetLine by line:
<string>declaresstd::string, a piece of text that grows as needed and frees its own memory. Unlike text in C, you never allocate or free it yourself.std::printisstd::printlnwithout the newline, so your answer appears on the same line as the question.std::string name;creates an empty string variable.std::getline(std::cin, name);reads one whole line from the keyboard (std::cin) intoname, spaces included. The>>operator, which you will see in older examples, stops at the first space, soAda Lovelacewould becomeAda.name.empty()andname.size()are member functions: functions that belong to the string and are called with a dot.
The printf ... | line feeds the answer in for you. To type it yourself, run ./greet on its own.
Check it worked
The piped run prints What is your name? Hello, Ada Lovelace! Your name has 12 characters. Running ./greet and pressing Enter without typing anything prints Hello, stranger!.
Troubleshooting
size() counts bytes, not letters. A name with accented or non-Latin letters, such as Adébáyọ̀ or Ngozi Ọ̀kọ̀, reports more characters than you see, because UTF-8 stores those letters in two or more bytes. That is expected; counting what a reader sees as letters needs a Unicode library.
Step 5 · Read a long compiler error from the first line
C++ error messages can run to dozens of lines. Most of that is the compiler listing every option it tried, often inside the standard library's templates (code written once to work with many types). Here is a deliberate mistake, printing a whole list of numbers with <<, which streams do not support:
cat > broken.cpp <<'CPP'
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores{70, 85, 92};
std::cout << scores << '\n';
}
CPP
g++ -std=c++23 broken.cpp -o broken 2> errors.txt || echo "Compile failed, as expected"
wc -l < errors.txt
grep -m1 "error:" errors.txt2> errors.txt saves the error messages to a file so you can count them; normally you read them in the terminal. wc -l counts the lines, and grep -m1 "error:" prints only the first error.
That first error is the one that matters. It names the file, line and column (broken.cpp:6:15) and says something like no match for 'operator<<', with the two types involved: a stream on the left and std::vector<int> on the right. Everything after it is the compiler explaining each operator<< it considered and why none fit. Fix the first error, compile again, and repeat; later errors are often side effects of the first.
Check it worked
wc -l prints a number well above 20 for a one-line mistake, and the grep line shows a single error pointing at broken.cpp:6. To fix it, print each number in a loop: for (int s : scores) std::println("{}", s); (with #include <print>).
Common beginner mistakes
- Running
./hello.cppinstead of./hello. The.cppfile is source text; only the compiled file runs. - Editing the source and running the old program. Compile again after every change.
- A missing
;at the end of a statement. g++ often reports it on the next line, so look one line up. - Forgetting an
#include. An error such as'string' is not a member of 'std'usually means the header for that name is missing. - Leaving out
-std=c++23, as you saw in step 3.
Step 6 · Build with CMake
Typing g++ commands works for one file. Real projects have many files, libraries and settings, and they describe them once in a CMakeLists.txt. CMake reads that file and writes the build instructions for whatever compiler and build tool the machine has, so the same project builds on Linux, macOS and Windows.
Write a build file for the greeting program:
cat > CMakeLists.txt <<'CMAKE'
cmake_minimum_required(VERSION 3.25)
project(hello_cpp LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(greet greet.cpp)
target_compile_options(greet PRIVATE -Wall -Wextra)
CMAKEWhat each line does:
cmake_minimum_requiredstates the oldest CMake that can build the project.projectnames it and says it is C++ (CXX), so CMake looks for a C++ compiler.- The three
setlines are the build-file version of-std=c++23: use C++23, fail rather than fall back to an older standard, and use plain standard C++ rather than GNU extensions. add_executable(greet greet.cpp)says to build a program calledgreetfromgreet.cpp. A larger program lists more.cppfiles here.target_compile_optionsadds the warning flags to that program only.
Configure the build in a separate build folder, then build:
cmake -S . -B build
cmake --build build
printf 'Grace\n' | ./build/greet-S . says the source is here and -B build puts everything CMake generates in build/, so your source folder stays clean and you can delete build/ at any time to start fresh. You run the first command once; after editing code, cmake --build build alone recompiles only what changed.
Check it worked
The configure command ends with -- Build files have been written to: and a path ending in build. The build prints a line such as [100%] Built target greet, and the last command prints What is your name? Hello, Grace! Your name has 5 characters.
When you are done, remove the tutorial folder:
cd ~
rm -rf ~/hello-cpp-demoNext steps
- See where C++ came from and what it adds: Hello World in C.
- Compare a newer systems language with a package manager and build tool in one: Hello World in Rust.
- Try another C-family language with a quicker edit-compile loop: Hello World in D.
- Add more
.cppfiles toadd_executableand splitgreet.cppinto a function in its own file. The CMake tutorial at cmake.org/cmake/help/latest/guide/tutorial covers the next steps.
Related tutorials
- Hello World in Zig: zig run, zig build and zig ccYou will install a signature-checked Zig 0.17.1 on Ubuntu 26.04, write and run Hello World with the current I/O API, build a small project with zig build that handles errors explicitly, and use zig cc to compile a C program for Linux and for Windows.
- Hello World in x86-64 assembly with NASM and Linux system callsYou will write an x86-64 assembly program that prints a line using only Linux system calls, assemble it with NASM, link it with ld, check its exit status, then loop it three times and read its machine code with objdump.
- Hello World in WebAssembly text format with wabt and WasmtimeYou will write a WebAssembly module by hand in the text format, assemble it with wat2wasm, print Hello World through WASI in Wasmtime 50, and call an exported add function from both Wasmtime and Node.js.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials