3.1 Why C++ for embedded systems, and where Rust fits

The code in this book is C++ on both tiers: C++17 or C++20 on the Raspberry Pi gateway, and C++17 on Zephyr running on the ESP32. Where a platform API is C (the Linux kernel, Zephyr’s driver APIs, libgpiod’s core library), we call it from C++ or, for kernel modules in Chapter 10, write C. Rust appears in option boxes throughout, and Section 3.10 shows how the two languages call each other.
This section explains why. You already know the language from EEN1097, so the focus here is on what changes when the code runs on a board rather than a desktop.
What a language costs on a small processor
Section titled “What a language costs on a small processor”In Exploring Raspberry Pi I ran the same n-body simulation (5 million iterations, one core, the same algorithm and the same answer) in eleven languages on a Raspberry Pi 3.1 Table 3.1 shows the times relative to C++.
Table 3.1. Relative run time of one n-body benchmark on a Raspberry Pi 3 at 1.2 GHz, 32-bit Raspbian, 2016 (C/C++ = 1.00, 6.5 s).
| Language | Relative time | How it runs |
|---|---|---|
C/C++ (-O3) | 1.00 | Compiled ahead of time to machine code |
| C++11 | 1.06 | Compiled ahead of time |
| Haskell | 1.16 | Compiled ahead of time |
| Java | 1.52 | Bytecode, JIT-compiled |
| Mono C# | 2.72 | Bytecode, JIT-compiled |
| Cython (typed) | 2.74 | Python translated to C |
| Node.js | 2.76 | JIT-compiled |
| Lua | 20.2 | Interpreted |
| Perl | 92.6 | Interpreted |
| Python | 94.1 | Interpreted |
| Ruby | 147 | Interpreted |
The exact numbers are ten years old, but the shape has not changed. Ahead-of-time compiled languages sit within a few tens of percent of each other. Interpreted languages are one to two orders of magnitude slower on the same core. On a desktop that gap is often hidden by a fast processor. On a gateway it decides whether one Pi can serve fifty sensor nodes or five, and on the 160 MHz ESP32-C62 (240 MHz on the ESP32-S3), with 512 KB of SRAM, an interpreter may not fit at all.
Speed is only one of the costs. On an embedded target I also care about:
- Memory footprint. A C++ program on Zephyr with no heap allocation has a RAM use you can read from the linker map at build time. A garbage-collected runtime needs headroom you cannot bound as easily.
- Start-up time. A compiled binary is running in milliseconds. A Python interpreter on a Pi 4 takes a noticeable fraction of a second to import its modules, which matters for a service that restarts after a fault (Chapter 2.6).
- Timing predictability. A garbage collector or a JIT compiler can pause the program at a point you did not choose. That rules them out for the control loops in Chapter 11.
- Access to the platform. The Linux system call interface, glibc, libgpiod and Zephyr’s APIs are all C. C++ calls them directly with no binding layer.
None of this means Python has no place. On the gateway I use Python for test scripts, data analysis and the bench harness in Section 3.9, where development time matters more than run time. Section 3.10 shows how to wrap a C++ driver so a Python script can call it.
Which C++ on which tier
Section titled “Which C++ on which tier”C++ is a large language, and the two tiers use different subsets of it.
Table 3.2. C++ features on the gateway and on the leaf node.
| Feature | Raspberry Pi gateway (Linux, glibc, libstdc++) | ESP32 leaf node (Zephyr) |
|---|---|---|
| Standard | C++17 or C++20 (GCC 14 defaults to C++17)3 | CONFIG_CPP=y to enable C++, then CONFIG_STD_CPP17=y (C++20 is available; Zephyr defaults to C++11)4 |
Heap (new, std::vector growth) | Yes | Avoid after start-up; prefer static allocation and fixed-size containers |
| Exceptions | Yes | Off by default; CONFIG_CPP_EXCEPTIONS=y costs flash and RAM |
RTTI (dynamic_cast, typeid) | Yes | Off by default; CONFIG_CPP_RTTI=y |
| Full standard library | Yes | Needs CONFIG_REQUIRES_FULL_LIBCPP=y |
| Threads | std::thread, std::mutex, POSIX threads | Zephyr kernel threads (k_thread), k_mutex (Chapter 11) |
RAII, classes, templates, constexpr, std::array | Yes | Yes, and these are the features worth using |
The features in the last row cost nothing at run time, and they are the reason I use C++ on a microcontroller at all. constexpr moves calculations to compile time. Templates give type-safe code with no virtual call. RAII ties a hardware resource to the lifetime of an object.
RAII and hardware resources
Section titled “RAII and hardware resources”RAII (resource acquisition is initialisation) means that an object acquires a resource in its constructor and releases it in its destructor. In Exploring Raspberry Pi I wrote an LED class that exported a GPIO pin in its constructor and unexported it in its destructor. Two objects, on GPIO4 and GPIO17, were created in that order, and when main() returned the program printed:
Destroying the LED with GPIO number 17Destroying the LED with GPIO number 4C++ destroys objects in the reverse order of construction,5 so the pin acquired last is released first. For hardware that order matters: a motor driver should be disabled before the supply it depends on, and a bus transaction should finish before the bus is released. The class in that book used the sysfs GPIO interface, which is now deprecated (Chapter 6).6 Chapter 6 rebuilds the same class on libgpiod v2, where the gpiod::line_request object owns the lines and releases them when it goes out of scope.
Readable code costs nothing
Section titled “Readable code costs nothing”A habit I see in students who have read about embedded optimisation is writing dense code to make the binary smaller: single-letter names, no comments, expressions packed onto one line. Comments and names do not reach the binary at all, and at -O2 or -O3 the compiler produces the same machine code for a readable version as for a packed one. You can check this yourself with objdump once you have read Section 3.2. Write the clear version, measure, and only then optimise the part the measurement points to.
Where Rust fits
Section titled “Where Rust fits”Rust compiles ahead of time through LLVM,7 so its performance is in the same group as C++ in Table 3.1. Its advantage is that the compiler rejects whole classes of memory and data-race errors (use-after-free, buffer overruns in safe code, unsynchronised shared state) that C++ accepts and that you then find with the debugger in Section 3.6.8
In late 2026 the position on our two tiers is:
- On the gateway, Rust is a mature option. The
gpiocdevcrate wraps the same character-device GPIO interface as libgpiod,9 and cross-compiling for the Pi is onerustup target add aarch64-unknown-linux-gnuaway (Section 3.2).10 - On the ESP32, there are two routes. Espressif’s
esp-hal11 with the Embassy async framework runs Rust with no operating system; I built a web server that way in an Embassy article on derekmolloy.ie. Zephyr’s own Rust support (thezephyr-lang-rustmodule) lets Rust applications run on the Zephyr kernel, but it covers a smaller part of the Zephyr API than C and C++ do.12 - In the Linux kernel, Rust is accepted for new drivers,13 but the C API is still the one you will meet in existing code (Chapter 10).
My summary of the state of the ecosystem is in The state of embedded Rust in 2026. For this module, C++ is the language the labs and assignments use, and Rust is accepted where a lab says so.
What this section does not cover
Section titled “What this section does not cover”C++ syntax, pointers, classes and the standard library are covered in EEN1097 and are not repeated here. A few points matter more on the leaf node than on a desktop, and each causes a fault that is hard to find later:
- Type sizes change between the tiers.
longis 64 bits on the Pi’s 64-bit Pi OS and 32 bits on the ESP32.14 Use the fixed-width types (int32_t,uint16_t) for anything that crosses between the tiers or matches a register. - The heap fragments. A node that runs for months and allocates and frees blocks of different sizes can fail to find a large enough block even when plenty of memory is free in total. Allocate at start-up, or use fixed-size pools.
- Static constructors run before
main(), in an order you do not control between files.15 A global object whose constructor uses another file’s global may see it before it is constructed. <iostream>is large. On Zephyr it pulls in a large part of the standard library; useprintk()or the logging subsystem (Section 3.6).- Shared memory with hardware or an interrupt needs
volatile, and Section 3.2 shows what the compiler does without it. - Precedence:
*p++reads*pand then advancesp;(*p)++increments the valueppoints to.
🧩 Knowledge Check
Section titled “🧩 Knowledge Check”Which of these C++ features cost nothing at run time on the ESP32 under Zephyr?
Why is a garbage-collected language a poor fit for the control loops in Chapter 11?
Footnotes
Section titled “Footnotes”-
D. Molloy, Exploring Raspberry Pi: Interfacing to the Real World with Embedded Linux. Indianapolis, IN, USA: Wiley, 2016, ISBN 978-1-119-18868-1. Available: wiley.com. ↩
-
Zephyr Project, ESP32-C6-DevKitC, board documentation. Available: docs.zephyrproject.org. ↩
-
GNU Project, C++ Standards Support in GCC (C++17 is the default mode from GCC 11 to GCC 15). Available: gcc.gnu.org/projects/cxx-status.html. ↩
-
Zephyr Project, C++ Language Support. Available: docs.zephyrproject.org. ↩
-
cppreference.com, Destructors. Available: en.cppreference.com/w/cpp/language/destructor. ↩
-
The Linux Kernel documentation, GPIO Sysfs Interface for Userspace (obsoleted by the GPIO character device userspace API). Available: docs.kernel.org. ↩
-
Rust project, Code generation, Rust Compiler Development Guide. Available: rustc-dev-guide.rust-lang.org. ↩
-
S. Klabnik, C. Nichols and C. Krycho, The Rust Programming Language, ch. 16, “Fearless Concurrency”. Available: doc.rust-lang.org/book. ↩
-
gpiocdev crate documentation, docs.rs. Available: docs.rs/gpiocdev. ↩
-
Rust project, Platform Support, The rustc book (aarch64-unknown-linux-gnu is Tier 1 with host tools). Available: doc.rust-lang.org/rustc/platform-support.html. ↩
-
esp-rs, esp-hal: bare-metal (no_std) hardware abstraction layer for Espressif devices, GitHub repository. Available: github.com/esp-rs/esp-hal. ↩
-
Zephyr Project, zephyr-lang-rust: Support for Zephyr applications written in Rust, GitHub repository. Available: github.com/zephyrproject-rtos/zephyr-lang-rust. See also Rust Language Support, docs.zephyrproject.org. ↩
-
J. Corbet, “The (successful) end of the kernel Rust experiment,” LWN.net, Dec. 10, 2025. Available: lwn.net/Articles/1049831. ↩
-
cppreference.com, Fundamental types (data models: ILP32 and LP64). Available: en.cppreference.com/w/cpp/language/types. ↩
-
cppreference.com, Static Initialization Order Fiasco. Available: en.cppreference.com/w/cpp/language/siof. ↩
© 2026 Derek Molloy, Dublin City University. All rights reserved.