Skip to content

Introduction & Environment Setup

Welcome to the Modern C++ Masterclass! This course is designed to take you from the basics of C++ to the advanced features of C++20 and C++23, specifically tailored for development on the Raspberry Pi.

What is C++?

C++ is a general-purpose programming language created by Bjarne Stroustrup. It is an extension of the C programming language, adding object-oriented features, generic programming, and direct memory manipulation.

Key Characteristics: - Performance: C++ is compiled directly to machine code, making it extremely fast. - Control: It gives you granular control over system resources and memory. - Portability: Standard C++ code can run on almost any platform, from microcontrollers to supercomputers.

Setting up the Environment

To write modern C++, we need a compiler that supports the latest standards (C++20/23). Raspberry Pi OS (Bookworm) comes with GCC 12, which is excellent.

1. Install Build Tools

Open your terminal and install the essential tools:

sudo apt update
sudo apt install build-essential cmake gdb git
  • build-essential: Includes gcc, g++, and make.
  • cmake: A powerful build system generator.
  • gdb: The GNU Debugger.

2. Verify Installation

Check that you have a recent version of GCC:

g++ --version

The Compilation Process

For a detailed explanation of the build process (preprocessing, compilation, linking), please refer to Part 4: Build Systems. 1. Preprocessing: Handles directives like #include and #define. 2. Compilation: Translates preprocessed code into assembly code. 3. Assembly: Converts assembly code into machine code (object files). 4. Linking: Combines object files and libraries into a single executable binary.

Your First Program: "Hello, World!"

Let's analyze a simple C++ program.

1
2
3
4
5
6
7
// main.cpp
#include <iostream>

int main() {
    std::cout << "Hello, Modern C++!" << "\n";
    return 0;
}

Modern C++ Style Guide

To write clean and efficient C++, follow these best practices from the start:

1. Avoid using namespace std;

It might seem convenient, but it pulls the entire standard library into the global namespace, causing naming conflicts.

// Bad
using namespace std;
cout << "Hello"; // Is this std::cout or my_library::cout?

// Good
using std::cout;
cout << "Hello";

// Best (Explicit)
std::cout << "Hello";

2. Prefer \n over std::endl

std::endl not only prints a newline but also forces a buffer flush, which degrades performance. Use \n unless you strictly need an immediate flush.

3. The Future of Output: std::print (C++23)

C++23 introduces std::print (and std::println), a faster and more convenient way to print. Note: This requires a very recent compiler (like GCC 14). For now, we stick to std::cout.

1
2
3
// C++23 Style
// #include <print>
// std::println("Hello, {}", "World");

Building with CMake

While you can compile manually with g++ main.cpp -o app, CMake is the standard for managing complex projects.

Create a file named CMakeLists.txt:

1
2
3
4
5
6
7
8
cmake_minimum_required(VERSION 3.10)
project(ModernCpp)

# Specify the C++ Standard (C++23)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(hello main.cpp)

Build Steps

  1. Create a build directory (to keep source clean):
    mkdir build && cd build
    
  2. Generate build files:
    cmake ..
    
  3. Compile:
    make
    
  4. Run:
    ./hello
    

Basic Input/Output

Besides std::cout, we have std::cin for input.

#include <iostream>
#include <string>

int main() {
    std::cout << "Enter your name: ";

    std::string name;
    std::cin >> name; // Reads a single word

    std::cout << "Hello, " << name << "!" << std::endl;
    return 0;
}

Note: std::cin >> stops at whitespace. To read a full line, use std::getline(std::cin, name);.