Embedded C++ is a subset of the C++ programming language that is optimized for embedded systems use and supported by some compiler vendors. Abbreviated EC++.
Barr Group Principal Engineer Dan Smith discusses the pros and cons of using the C++ programming language in embedded systems.
- Webinar: How to Get Started with C++ in Embedded Systems
- How to Code a State Machine in C or C++
- How to Create Jump Tables via Function Pointer Arrays in C and C++
- CRC Series, Part 3: CRC Implementation Code in C/C++
Andrew Girson: Hi. I’m here today with Dan Smith, Barr Group Principal Engineer to talk about embedded C++. So, Dan, clearly C is the predominant language that is used in embedded systems. Why is C so popular in embedded systems?
Dan Smith: Yeah, that is a good question. Yeah, first thing I’ll say is that if people are wondering as far as statistics and data from our Embedded Systems Survey that we do every year and from training, I always ask the question who is using C and who is using C++. It is pretty clear it is an 80-20 split, 85-15 split. Most people are using C. Some people are using C++. So, it is dominant. And, I have a pretty good intuition on that from talking to people and I’ve had people tell me it is typically due to the fact that a lot of people who come in to embedded programming are coming from a EE background or a computer engineering background. And way back in the early days when they are first learning, those classes are typically taught using C programming language.
Dan: It is kind of, you know, C is kind of a high-level assembly language for all intents and purposes. And by the way, I’m never going to say anything about C. I love the language but it does have its sharp edges.
Andrew: It has its limitation, its issues.
Dan: Exactly, it does. But we as embedded engineers wanted to use it. So predominantly, I believe the reason is that is what people learn and that is what people and that is what people stick with and they learn to solve their problems using C. So, they don’t really find the need to go looking elsewhere for something else.
Andrew: Okay. But like you said, in our recent embedded safety and security survey, 20% are using embedded C++. So clearly, there are motivations or reasons to use it. What are the primary reasons why someone might use C++ or wanting to use C++ in their embedded project?
Dan: Right, right. Well, you know, so C++ is traditionally regarded by a lot of deeply embedded developers as a desktop language because that used to be the predominant domain for C++. It is only for desktop applications. But it is being used more and more in embedded work. And one of the main reasons for using C++ is that it – we like to say it gives you a bigger toolbox. So, the language itself, it is a bigger language.
Dan: It is not exactly a superset of C. That is not quite a correct way to characterize it, but it largely is. But it does give you a lot more things out of the box. So, people like to say batteries included.
Dan: And so you could be productive more quickly. I don’t have time to get into all those right now but that is an overarching answer. There are also some things that you can do using C++ that you actually cannot do in the C programming language due to features like templates or the magic of constructors and destructors which run automatically. You don’t have to write them and you can’t forget to write them. And you can also tailor C++ for an embedded system to make it smaller. There is a saying in the embedded systems – or really in the C++ community that you only pay for what you use. So, a lot of people believe that C++ is big and bloated but that is not necessarily the case.
Dan: So, yeah, those are the main reasons that people are using C++ than embedded, and I think that the percentage is slowly growing.
Andrew: So the – some of those features of C++ that may result in slower execution times or more memory usage or things like that, you can choose to avoid using them and use those features of the language that will benefit you without necessarily having to pay the price of using some of the features that might be fine for C++ in desktop or cloud-based computing but embedded computing are not.
Dan: Yeah, that is exactly right. Either by just not using the features and therefore they’re now built into your executable, or by explicitly disabling them at compile time and link time such as exceptions and runtime type information. Those are two features which are almost always disabled in embedded systems.
Andrew: Because of the impact they would have on the performance and the cost of the system and all of that.
Andrew: Okay. And is – are there certain types of projects or efforts where C++ is kind of a better fit than C in terms of embedded systems?
Dan: So I would say that C++ is almost always a good fit as long as the people on the group understand the language and can use it. But as far as where I would say where I see it most widely is there are usually two factors. One is as a project gets larger and larger, the benefits of some of C++’s features like name spaces and, you know, perhaps templates and other things which we won’t get into, they really start to shine in a bigger project. And so, I see it typically used in bigger projects, not to say it is not applicable on smaller project.
Dan: And then I also see it used in projects where the project leader perhaps is very strong in C++ and knows how to use it properly and once they agreed into the group because it is a good fit even if it is not a big project but that person knows that he or she can shepherd the usage of the language properly and keep it from getting out of control and develop a good use. So, if one of those two factors is at play or both, that is when I intend to see the use.
Andrew: Okay, great, great. Any closing thoughts?
Dan: The only thing I’ll say actually, two different points. One is we do have a webinar at Barr Group on C++. A colleague, Michael Wilk did a very good webinar a few months ago on that. I believe that is available either through our website –.
Andrew: That is on our website, sure.
Dan: Okay, great. And I would totally recommend anybody to watch that video if this is interesting. And then the other thing is that both in our Embedded Software Boot Camp and in our Embedded Security Boot Camp, we discuss usage of the language. Particularly in the security boot camp, we actually have two hands-on exercises that use specific features of the language that you can’t do in C that it improved the security of your software. So, I think that is a good compelling reason to use it. And that is in our security boot camp.
Andrew: Okay, great. Well, thank you, Dan.
Dan: You’re welcome.