Post

My Article in Programmer Issue 11

Issue 11 of Programmer magazine has a special feature commemorating the 20th anniversary of C++. I contributed an article titled “The Application of C++ in Embedded Systems.” Despite the grand title, space was limited, so I could only give a broad overview — somewhat content-free. Posted here for your critique. (One diagram is missing — if you want to see it, buy the magazine. Hehe.)


C++ in Embedded Systems By He Zongjian Embedded Systems Research Center, School of Software Engineering, Tongji University

Changing the Stage

C++ has played a critically important role in the history of the software industry since its birth. Its powerful language features and flexible mechanisms have won the admiration of countless programmers. The OOP and GP paradigms embedded in C++ have subtly influenced software architecture methods. At this level, C++’s influence goes far beyond being just a programming language. However, with the rise of the Internet and B/S architecture software, emerging languages like Java, C#/VB.NET (based on virtual machines) and dynamic languages like Ruby/Groovy — with their rapid development and ease of use — have begun challenging C++’s position, occupying an increasingly important place in enterprise computing. Putting enterprise computing aside, let’s change the stage and look at C++ in another fiercely competitive domain.

Embedded Systems Programming

“Embedded” is a broad topic covering a wide range of areas, from hardware design to software development. Generally, embedded systems have three characteristics: embeddedness, specialization, and computation. Embeddedness means the system is embedded into other target systems — from calculators and MP3 players to missiles and space shuttles. Specialization means the hardware and software must be customizable. Computation means the system must meet the target system’s control requirements. Due to the diversity of application environments, embedded system hardware and software come in many varieties. Processors range from 4-bit to 128-bit, memory from a few KB to hundreds of MB or even GB.

For a long time, embedded software conjured images of painfully slow processors and meager memory/storage — resource-constrained environments. This is still and will remain true for a long time. In resource-constrained environments, code size and execution efficiency are typically the programmer’s top concerns. For this reason, C and even assembly language have long dominated the embedded market.

C++ didn’t flourish in embedded from the start for good reasons. First, the sheer variety of embedded applications means we’ll never use ISO C++ on a 4-bit chip to control blinking lights (though it’s possible). That kind of work needs just a few assembly instructions — using C++ would be like using a anti-aircraft gun to swat mosquitoes. More importantly, the ISO C++ standard defines syntax but not runtime overhead. In practice, some compilers’ implementations of certain ISO C++ features cause code bloat and poor efficiency. This has led some embedded developers to avoid C++.

Making C++ Serve Embedded Systems

Despite the potential overhead, C++ can still produce excellent embedded software by playing to its strengths — encapsulation and inheritance — while avoiding problematic language features.

The relationship between C++ language features, their importance in OOP, and their implementation cost can be visualized. When programming in resource-constrained environments, developers need to balance and make trade-offs among these features.

Since some ISO C++ features can cause performance and code size issues, adapting C++ for embedded use would be ideal. The Embedded C++ Technical Committee was established in the 1990s, and the Embedded C++ (EC++) specification was released in 1998.

EC++ is a subset of standard C++, removing features like multiple inheritance, virtual base classes, RTTI, exception handling, templates, and namespaces. On the library side, STL and Stream are removed. Many embedded vendors have released EC++-compliant compilers.

New C++ Applications

In recent years, the embedded systems field has seen new developments. First, consumer electronics like phones and PDAs have driven explosive market growth. Second, the Internet has pushed more embedded applications to require network support, driving hardware enhancement and software complexity. C++ has found new applications here.

In embedded SoC design, SystemC (released by OSCI in 1999) is an open system-level design language built on C++ that enables natural and effective SoC architecture description — allowing C++ to contribute in chip design.

As embedded software grows more capable, full-featured operating systems have appeared in embedded systems. Common embedded OSes include VxWorks, embedded Linux, Windows CE, etc. In this space — especially high-end consumer devices — C++ finds ample opportunity. Many embedded OSes are built with C++ and provide C++ APIs.

Symbian OS is a good example — its APIs are all C++ classes. Microsoft’s Windows CE and Windows Mobile platforms use Embedded Visual C++, which shares the look and feel of desktop Visual C++ and includes MFC, ATL libraries — easing the transition for desktop developers.

In China, Kotei Century’s Elastos (和欣) embedded OS is built entirely with C++ and introduces concepts like metadata not found in ISO C++, allowing self-describing components in C++ — making runtime reflection, component self-description, and binary inheritance no longer exclusive to Java/C#.

Even in electronic toys, C++ appears. BrickOS is an open-source embedded OS for Lego robots, providing a C/C++ environment for controlling sensors and motors.

Conclusion

C++ was designed as a general-purpose language from birth. After 20 years, it’s widely used on servers and workstations. With the rapid rise of mobile devices and embedded systems, C++ has extensive application in these domains. As a system-level language, C++’s superior features can shine on embedded devices. As the embedded industry grows and C++ continues to improve, C++ will have an even brighter future in the embedded domain.

This post is licensed under CC BY 4.0 by the author.