C++ for Embedded: Advantages, Disadvantages, and Myths

Engineers use the C computer language for a range of embedded systems. But experts say C++ can be a better option. I've gathered some tips from experts, such as my colleague Tino Pyssysalo, on when C++ can work especially well in an embedded system.

In this article:

What is embedded C++?

Embedded C++ is a dialect of C++ that engineers developed in the late 1990s for embedded systems. Embedded engineers seldom use the language anymore. They do use C++ in embedded systems a lot.

What is the difference between embedded C++ and C++?

Embedded engineers developed "embedded C++" for embedded systems with as low as 4kB of RAM memory and a microcontroller processor. The language tried to keep valuable C++ concepts while removing features that increased memory requirements and reduced the processor’s efficiency.

What are the features of embedded C++?

Embedded C++ attempted to keep the most helpful aspects of C++ for embedded systems while eliminating those that caused undeterministic CPU cycles or boosted memory requirementsEmbedded C++, for instance, retained how C++ uses classes as a blueprint to create objects for object-oriented programming. 

However, embedded C++ removed features that embedded engineers use today. Those elements include multiple inheritance, templates, exceptions, virtual base classes, and runtime type identification.

Why C++ is good for embedded systems

C++ works well for embedded programming because it sits close to the system hardware. C++ has pretty much everything that C does, but much more: an array of higher-level language features, including object-oriented programming and type-safe cast that helps to avoid unpleasant memory access errors.

Advantages of C++ for embedded systems

  • Ease of use: C++ uses object-oriented programming and has a range of templates and tools that make it reliable and easier to build reusable parts of code.

    Jacob Beningo, Embedded Software Consultant and President of Beningo Embedded Group, says he likes C++ in embedded systems for its scalability and how easy it is to reuse some of its code. Its templates, for example, allow engineers "to create multiple objects based on very similar types of behavior. They give you a great way to reuse and scale your software."

    C++ also uses the concept of inheritance, Beningo adds. “That allows engineers to create a baseline object in the language that will always have some minimum set of features. Then you inherit from that base class and build your customizations. So C++ gives you a way to a very flexibly design and build software systems."
  • Portability: Since parts of C++ code are easily reusable, the code is also portable from one device to another.
  • Standard library: The C++ standard library provides engineers with tools to create optimal code easily.

    Beningo says an important advantage is engineers can leverage what other engineers have already produced in a standard library of basic C++ features. "Instead of writing all that from scratch, you just inherit from those libraries, and then you customize and build your pieces onto it,” Beningo explains.
  • Stability: Programs written in C++ in an embedded system can operate for decades without fail because of the language’s stability.
  • Gateway language: Once your engineering team learns C++ well, it will be easier for them to learn and understand languages like Python, Java, JavaScript, and others.
  • Support: C++ is used widely, with a large support community, and updated often. “C++ has lots of support," Beningo shares. "It's updated and improved every few years."
  • Good for GUIs: C++ offers especially good tools for embedded systems with graphical user interfaces (GUIs).

    Tino Pyssysalo, a Senior Product Manager for Qt, agrees with other experts who say C++ also offers tools and techniques that make it an excellent option when embedded systems need a GUI.

    "It will be much easier to implement graphical user interfaces using the object-oriented programming of C++,” Pyssysalo advises.

    "C++ is continuously evolving. It's more forward-looking, and there are more and more nice features, which are highly optimized by compilers. So, they are fast and memory efficient.

    In addition, there’s the active standardization work that’s being conducted. A new C++ specification is out every third year, C++20 being the latest one. More and more modern programming concepts are added to the language"

What are the disadvantages of C++ for embedded?

Engineers will find that C++ has some disadvantages for embedded systems. The language is complex and difficult to learn. Memory allocations in C++ can also cause problems if engineers don't understand it.

Here are details on some disadvantages:

  • Hard to learn: C++ is a complex language that is challenging to learn. "The language is more complicated than C," Pyssysalo states. "There are more constructs, such as virtual functions and template classes, so more things to learn."

    That complexity means many developers don't have the same understanding of C++ as they do of C. Beningo shares, "Developers don't understand its uses as well for real-time systems as they do C."
  • Codebase obstacles: The language’s complexity makes maintaining the codebase challenging. Some companies that have built a system in C++ are reluctant to make any changes because they don't want to risk causing failures or major problems in the system.
  • Potential performance issues: C++ can run more slowly than C if certain features in C++ are enabled, including exceptions or runtime type information (RTTI).

    Beningo advises that engineers should be careful to avoid using C++ templates that are too complex—more complex than their system needs. "If you get overly complicated or do too much, you can end up with performance issues. You've got to be careful in how you use templates,” he adds.
  • Memory management challenges: With C++, the programmer controls memory management, which can be a disadvantage. Many languages have a garbage collector to help manage memory that automatically frees up unneeded memory. C++ doesn't have that feature, so programmers must control it manually. Memory errors can cause serious bugs and even crashes.

    "Memory allocations can create confusion and result in a buggy or poor-performing system," Beningo confirms.

    There are plenty of good tools to detect memory leaks in C++ software.

Pros & Cons of Using C++ in Embedded Systems

Advantages of C++

Disadvantages of C++

Its object-oriented programming, templates, and other tools make it easier to build reusable parts of code

It is a complex language that’s difficult to learn

Easily portable from one device to another

Maintaining code can be challenging

Deep C++ standard library provides engineers with tools to more easily create optimal code

Can run more slowly—only if certain features are enabled that don’t need to be 

Extremely stable language

Programmers must control memory manually, which can cause errors, bugs, and even crashes

Once engineers learn C++, it’s easier to learn Python, Java, JavaScript, and others

 

C++ is used widely, has a large support community, and updated often

 

C++ has good tools for embedded systems with GUIs

 

C++ is being frequently standardized

 

 

Debunked Myths about C++

Experts say myths have often existed about using C++ for embedded systems. Myths include that C++ performs too slowly. That’s not true with properly written C++ code. 

Here are some C++ myths and what’s the real story:

Myth: C++ produces bloated machine code, and C++ operates too slowly for embedded systems

Reality: Engineers can avoid bloated machine code with C++, and the language can run as quickly and efficiently as C. 

Beningo explains, “Engineers can create code bloat and have performance issues if they're not careful. There are capabilities in the language that if you don't use a subset of it, you can really destroy your performance in an embedded system."

The crucial point is to avoid using some C++ features that can cause that bloat and aren't necessary in embedded systems, Beningo stresses. 

Myth: C++, as a whole, doesn't work well in embedded systems.

Reality: C++ can work very well in many embedded systems, Beningo says, "as long as the developers know what they are doing and carefully monitor performance and memory usage." 

Is C++ better than C for embedded systems?

Some experts believe C++ is often better for embedded systems. Others believe C is a better choice. Most pros say your choice of language depends on the system and how you use the language.

Beningo believes C++ is often better in many embedded systems. "You can leverage reuse, scalability, polymorphism, inheritance to design and build more scalable solutions."

"There is no absolute truth,” Qt's Pyssysalo points out. “It really depends on the use case and what you are developing."

Learn more about the wide range of programming languages that engineers can use in various embedded systems. You can also get insights and advice on creating an effective embedded system design process.

When you need embedded C++ 

Beningo believes C++ can work as well as or better than C in a wide range of embedded systems. C++ language's advantages are the templates, standard libraries, and similar tools that make it valuable in complex systems that engineers may want to rebuild across other formats and product lines. "If you're looking at a complex system and you say: We're going to scale it, we're going to use it across multiple product domains ... then you really should be using C++," he adds.

Should you learn C or C++ for embedded?

Both Beningo and Pyssysalo say engineers should learn both C and C++ if they want to work extensively in embedded systems.

"You need to know both: 80% of (embedded) systems are still written in C, so you have to know it,” Beningo advises. “But the remaining 20% percent is mostly C++."

Pyssysalo says he would definitely focus on learning C++ because when an engineer learns C++, they’re also learning most of C. "C++ is a superset of C. In learning C++, you have to know the C concepts,” he shares. “In the coming years, as engineers look to move among companies that produce embedded systems, knowing C++ will provide better markets for you to apply to a new job."

You can learn more about the range of skills an embedded engineer should learn for career success.

Is C++ the right language for your embedded project?

Experts say whether C++ is the right language for your embedded project depends on the details of your embedded system and what it needs to perform well.

Pyssysalo explains that the smallest microcontrollers with minimal memory in embedded systems often use C. However, embedded systems with powerful processors and larger memory limits—especially those with a GUI—are better candidates for C++.

Comparison of the top 6 embedded software programming languages

Here’s an overview of where the various programming languages excel and fall short based on need. You can read a detailed comparison of the top 6 embedded programming software languages, including C and C++. 

 

  C++  Java  Python /
MicroPython
JavaScript/
HTML5/CSS 
Rust
Strengths 

Embedded, bare-metal, IoT Embedded, bare-metal,
standalone
apps, IoT
Web, cloud  Cloud, data science  Web  Embedded,
bare-metal
Ease of development  ★★★  ★★  ★★★  ★★★★★  ★★  ★★★★
Expressive power ★★★★ ★★ ★★★★★ ★★★ ★★★★
Ease of maintenance ★★★★ ★★★★ ★★★★ ★★★ ★★★★
Longevity ★★★★★ ★★★★ ★★★ ★★★
Runtime efficiency ★★★★★ ★★★★★ ★★★ ★★ ★★★★
Library/module availability ★★★★ ★★★★ ★★★ ★★★★ ★★★★ ★★
Low-level interface ★★★★★ ★★★★★ ★★ ★★★ ★★★★
Connectivity support ★★ ★★★/★★★★★ 1 ★★★★ ★★★★★ ★★★★★ ★★
Graphics support ★★★★ ★★★★★ ★★★

★★★

★★★★★
Developer community

★★★

★★★ ★/★★★★2 ★★★★ ★★★★★ ★★

 

★ is the lowest ranking while ★★★★★ is the highest. 
1 Provides high level of connectivity and networking support when platform libraries included
2 Java Developer community exclusive of Android (low) and inclusive of Android (high)

Make sure you think about your GUI when choosing a programming language for an embedded system

Every embedded programming language has its advantages and disadvantages. And each language has different capabilities when it comes to handling the GUI requirements in some embedded systems. If your system needs a GUI, it's essential to understand a programming language's ability to make that work well.

Qt helps build your embedded system in C++ and beyond

Engineers who use C++ in embedded systems know that a major advantage of choosing that language is its extensive standard library. Qt includes a comprehensive set of C++ library classes to help you build an embedded system. It also offers a range of other features that make it the best solution to build embedded systems for connected devices with 3D and 2D user interfaces.

You can learn more about how to easily create a GUI for embedded devices using the declarative language QML and Qt Quick. Qt’s embedded product planning and requirements guide helps engineers understand how to approach building an embedded device. The handbook includes a detailed comparison of some of the top embedded software languages. You’ll also find a comparison table to help you choose the best technologies and tools to build your user interface.

 


 

embedded_guide (1)

The Embedded Product Planning and Requirements Guide

In this guide for IoT and embedded product planning, we examine the most important criteria to consider at the outset, compare a list of the most used technologies, and rate them in easy-to-compare categories.

Download

 


Blog Topics:

Comments