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

Unlike interpreted languages (like Python), C++ code must be compiled before it can run. This process has four main stages:

  1. Preprocessing: Handles directives starting with # (like #include). It copies header files into your source file.
  2. Compilation: Translates C++ code into assembly language.
  3. Assembly: Translates assembly into machine code (object files .o).
  4. Linking: Combines object files and libraries into a single executable binary.

Your First Program: "Hello, World!"

Let's analyze a simple C++ program.

// main.cpp
#include <iostream>  // 1. Preprocessor directive

// 2. Main function: The entry point of the program
int main() {
    // 3. Standard Output
    std::cout << "Hello, Modern C++!" << std::endl;

    // 4. Return statement
    return 0; 
}

Code Breakdown

  • // ...: These are comments. The compiler ignores them. - // for single-line comments. - /* ... */ for multi-line comments.
  • #include <iostream>: Tells the preprocessor to include the standard Input/Output stream library. This allows us to use std::cout.
  • int main() { ... }: Every C++ program must have exactly one main function. Execution starts here.
  • std::cout: Represents the Standard Output (usually the terminal).
  • <<: The insertion operator. It sends the string "Hello..." to cout.
  • std::endl: Inserts a newline character and flushes the buffer.
  • return 0;: Returns the exit status to the operating system. 0 typically means "success".

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);.