Zum Hauptinhalt springen
Nicht aus der Schweiz? Besuchen Sie lehmanns.de

C++ in Embedded Systems (eBook)

A practical transition from C to modern C++
eBook Download: EPUB
2025 | 1. Auflage
402 Seiten
Packt Publishing (Verlag)
978-1-83588-115-6 (ISBN)

Lese- und Medienproben

C++ in Embedded Systems -  Amar Mahmutbegovic
Systemvoraussetzungen
28,79 inkl. MwSt
(CHF 28,10)
Der eBook-Verkauf erfolgt durch die Lehmanns Media GmbH (Berlin) zum Preis in Euro inkl. MwSt.
  • Download sofort lieferbar
  • Zahlungsarten anzeigen

Transitioning from C can be daunting, with concerns about performance overhead, added complexity, and unfamiliar tooling. Addressing these challenges, Amar Mahmutbegovic, an advocate for modern C++ in embedded development, shows you how to harness zero-cost abstractions, compile-time checks, and powerful modern C++ capabilities to preserve performance while achieving safer, cleaner code. This book bridges the gap between traditional C and advanced C++, helping you retain the efficiency C developers demand while unlocking the safety and expressiveness of modern C++. Starting with a modern development environment setup, including a Docker container for seamless example replication, you'll overcome the hurdles of using the C++ standard library in memory-constrained settings and get acquainted with the Embedded Template Library (ETL) as an alternative. The book walks you through essential C++ concepts before exploring advanced topics such as templates, strong typing, error handling, compile-time computation, and RAII. Through practical examples, you'll implement a sequencer, write a type-safe HAL, and apply patterns like Command, State, and Observer to solve common embedded development problems. By the end of this book, you'll have learned how to apply modern C++ to develop robust, modular firmware with performance matching or exceeding hand-coded C solutions.


Go beyond C by applying modern C++ in embedded systems to enhance code readability, maintainability, and scalabilityKey FeaturesBridge the gap between C and modern C++ for embedded systems through practical examplesLearn how to save memory and cut down on runtime computing using compile-time computation techniquesImprove your software design skills by applying patterns to solve common problems in embedded systems using C++Purchase of the print or Kindle book includes a free PDF eBookBook DescriptionTransitioning from C can be daunting, with concerns about performance overhead, added complexity, and unfamiliar tooling. Addressing these challenges, Amar Mahmutbegovic, an advocate for modern C++ in embedded development, shows you how to harness zero-cost abstractions, compile-time checks, and powerful modern C++ capabilities to preserve performance while achieving safer, cleaner code. This book bridges the gap between traditional C and advanced C++, helping you retain the efficiency C developers demand while unlocking the safety and expressiveness of modern C++. Starting with a modern development environment setup, including a Docker container for seamless example replication, you'll overcome the hurdles of using the C++ standard library in memory-constrained settings and get acquainted with the Embedded Template Library (ETL) as an alternative. The book walks you through essential C++ concepts before exploring advanced topics such as templates, strong typing, error handling, compile-time computation, and RAII. Through practical examples, you'll implement a sequencer, write a type-safe HAL, and apply patterns like Command, State, and Observer to solve common embedded development problems. By the end of this book, you'll have learned how to apply modern C++ to develop robust, modular firmware with performance matching or exceeding hand-coded C solutions.What you will learnDebunk myths and misconceptions about using C++ in embedded systemsSet up build automation tailored for C++ in constrained environmentsLeverage strong typing to improve type safetyApply modern C++ techniques, such as Resource Acquisition Is Initialization (RAII)Use Domain Specific Language (DSL) with a practical example using Boost SMLImplement software development best practices, including the SOLID principle, in embedded developmentWho this book is forThis book is for embedded developers who primarily use C and want to adopt a modern C++ approach. It introduces fundamental C++ concepts, making it suitable for beginners, while also assuming basic familiarity to fully leverage advanced features like compile-time computation. Even those with prior C++ experience will discover new ways to apply modern best practices to write more efficient and maintainable embedded applications.]]>

Preface


C++ is a general-purpose, multi-paradigm programming language, supporting procedural, object-oriented, and, to some extent, functional programming paradigms. It started out as C with classes, but over time it transformed into a modern language that enables writing highly expressive code without sacrificing performance. Despite this, C remains the dominant language in embedded development, primarily due to its simplicity and gentler learning curve.

However, the simplicity of C often makes writing complex systems overly verbose, increasing the cognitive burden on developers and making code more error-prone. This is where C++ excels. With features such as generic programming, runtime and compile-time polymorphism, compile-time computation, and enhanced type and memory safety, it is a superb choice for embedded system development.

Myths about C++, such as code bloat and runtime overhead, are still widespread. This book begins by debunking these misconceptions and guiding you through C++ fundamentals. It then shifts focus to more advanced modern C++ concepts, applying them to solve real-world problems in embedded development.

The goal of this book is to show you how modern C++ can be effectively used in embedded systems through carefully selected examples and by applying good software development practices.

Who this book is for


This book is for embedded developers who mainly use C in their daily jobs and would like to discover modern C++. Some familiarity with C++ is expected but not necessary, as the book also covers C++ basics.

What this book covers


Chapter 1, Debunking Common Myths About C++, explores widespread misconceptions about C++ and systematically debunks them. You will also gain insight into the history of C++ and the zero-overhead principle.

Chapter 2, Challenges in Embedded Systems with Limited Resources, examines the design challenges faced in resource-constrained embedded systems, with a focus on profiling techniques and memory management. It also shows how to avoid potentially problematic language features such as exceptions and RTTI.

Chapter 3, Embedded C++ Ecosystem, reviews the tools available for C++ development in the embedded domain, including toolchains, static analyzers, profiling tools, and testing frameworks.

Chapter 4, Setting Up the Development Environment for a C++ Embedded Project, walks you through setting up a modern development environment for C++ embedded projects, including using a simulator to test your code in a virtual setting.

Chapter 5, Classes – Building Blocks of C++ Applications, guides you through understanding classes in C++, including storage duration and initialization and inheritance and dynamic polymorphism.

Chapter 6, Beyond Classes – Fundamental C++ Concepts, covers fundamental C++ features such as namespaces and function overloading. It also discusses interoperability with C and introduces standard library containers and algorithms.

Chapter 7, Strengthening Firmware – Practical C++ Error Handling Methods, goes through various error handling techniques in C++, including error codes, asserts, and global handlers. It also explains the mechanics of exceptions and how they work.

Chapter 8, Building Generic and Reusable Code with Templates, goes through templates and concepts. It also provides an introduction to template metaprogramming and compile-time polymorphism.

Chapter 9, Improving Type-Safety with Strong Types, discusses implicit and explicit type conversions in C++ and introduces the concept of strong types. A practical example from an embedded library demonstrates how to improve type safety.

Chapter 10, Writing Expressive Code with Lambdas, introduces lambdas and shows you how to use them within a command design pattern to implement an expressive interrupt manager.

Chapter 11, Compile-Time Computation, explores C++’s compile-time computation capabilities and demonstrates how to use them to build a signal generator library that generates lookup tables at compile time.

Chapter 12, Writing C++ HAL, demonstrates the implementation of HAL in C++, using template-metaprogramming to ensure type-safety.

Chapter 13, Working with C Libraries, shows how to effectively use C libraries in C++ projects. It demonstrates the RAII principle in an example of using a filesystem C library.

Chapter 14, Enhancing Super-Loop with Sequencer, shows how to improve simple super-loop-based designs using a sequencer. It also introduces the Embedded Template Library (ETL) and its container class templates with fixed sizes known at compile time.

Chapter 15, Practical Patterns – Building a Temperature Publisher, guides you through the Observer design pattern and demonstrates how to apply it in systems such as thermostats and HVAC controllers.

Chapter 16, Designing Scalable Finite State Machines, explores different ways to implement finite state machines. It begins with a basic enum-switch approach, introduces the State design pattern, and then presents the Boost.SML library.

Chapter 17, Libraries and Frameworks, highlights parts of the C++ Standard Template Library that are useful for firmware development in constrained systems. It also features the CIB and Pigweed libraries.

Chapter 18, Cross-Platform Development, discusses the importance of good software design for achieving portability and testability in embedded software.

To get the most out of this book


Many examples in the book can be run in Compiler Explorer (https://godbolt.org/). Use it to observe the assembly output of the compiler. Experiment with the examples, tweak them, and compile them with different optimization levels and compiler flags to understand how those changes affect the compiler output.

Most of the examples can also be run in the Renode simulator. The book is accompanied by a Docker container, which includes the GCC toolchain and the Renode simulator, enabling you to run the code in an embedded target simulation.

Software/hardware covered in the book

Operating system requirements

Docker

Windows, macOS, or Linux

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files


The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Cpp-in-Embedded-Systems. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing. Check them out!

Download the color images


We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781835881149.

Conventions used


There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: “The PT100 class is also a TemperatureSensor class, and the TemperatureController class has a member (object) of TemperatureSensor and a PidController class.”

A block of code is set as follows:

#define N 20 int buffer[N]; for(int i = 0; i < N; i ++) { printf("%d ", buffer[i]); }

Any command-line input or output is written as follows:

The output of this simple program might be surprising: resistance = 3.00

Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: “Now, we need to add the Google Test library by clicking on the Libraries button in the execution pane.”

Warnings or important notes appear like this.

Tips and tricks appear like this.

Get in touch


Feedback from our readers is always welcome.

General feedback: Email feedback@packtpub.com and mention the book’s title in the subject of your message. If you have questions about any aspect of this book, please email us at questions@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our...

Erscheint lt. Verlag 2.7.2025
Vorwort Steve Branam
Sprache englisch
Themenwelt Informatik Programmiersprachen / -werkzeuge C / C++
Mathematik / Informatik Informatik Theorie / Studium
Technik Elektrotechnik / Energietechnik
ISBN-10 1-83588-115-7 / 1835881157
ISBN-13 978-1-83588-115-6 / 9781835881156
Informationen gemäß Produktsicherheitsverordnung (GPSR)
Haben Sie eine Frage zum Produkt?
EPUBEPUB (Ohne DRM)

Digital Rights Management: ohne DRM
Dieses eBook enthält kein DRM oder Kopier­schutz. Eine Weiter­gabe an Dritte ist jedoch rechtlich nicht zulässig, weil Sie beim Kauf nur die Rechte an der persön­lichen Nutzung erwerben.

Dateiformat: EPUB (Electronic Publication)
EPUB ist ein offener Standard für eBooks und eignet sich besonders zur Darstellung von Belle­tristik und Sach­büchern. Der Fließ­text wird dynamisch an die Display- und Schrift­größe ange­passt. Auch für mobile Lese­geräte ist EPUB daher gut geeignet.

Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür die kostenlose Software Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür eine kostenlose App.
Geräteliste und zusätzliche Hinweise

Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.

Mehr entdecken
aus dem Bereich

von Carsten Vogt

eBook Download (2024)
Carl Hanser Fachbuchverlag
CHF 38,95
C++ lernen - professionell anwenden - Lösungen nutzen

von Ulrich Breymann

eBook Download (2023)
Carl Hanser Fachbuchverlag
CHF 48,80