Is C++ Becoming Widely Used in Embedded Software Programming?

I am Lao Wen, an embedded engineer who loves learning.Follow me to become even better together!

In embedded Linux software development, do you experts prefer to use C or C++?

I think it depends on what language the lead developer prefers or is skilled in.

Is C++ Becoming Widely Used in Embedded Software Programming?

Here is a popular topic in the embedded field on Zhihu, let’s see what the experts say:

Is C++ Becoming Widely Used in Embedded Software Programming?

Original link:

https://www.zhihu.com/question/374663834

Several highly upvoted answers:

Answerer: pansz

The reality is: C++ is too difficult, and embedded talent is already scarce. If you also need to be able to use C++ without making mistakes, then it becomes even scarcer.

So using C is indeed mainstream. Because the requirements for C programmers are relatively lower.

I remember when I first started with embedded systems, the system didn’t even have an MMU, and all the code was in a single memory space. I had to manage the memory pool myself to avoid fragmentation. A random memory access error could affect completely unrelated modules. Would you dare to use C++ in such a system?

Conclusion: If you are developing code alone and are confident in your C++ skills, then using C++ is certainly fine. However, considering the overall C++ skill level of the programmer community compared to that of C, using C for embedded projects is more realistic.

Answerer: candy

As an embedded veteran with over ten years of experience, I can say that C++ is too complex, with too many language features. Implementing a function can be done in dozens of ways, with many bizarre methods to achieve a function. C++ features are so complex that without over five years of experience, you can’t use it well. But if a project team has inconsistent C++ skills, using some bizarre features to implement functions makes it impossible to maintain between multiple people.

During debugging, object-oriented debugging is best done with graphical tools, but embedded systems often lack such debugging tools. Writing business logic in C++ and debugging bugs later can be a nightmare; debugging C++ in embedded systems is an order of magnitude more complex than C.

While C has fewer features, it is completely sufficient. There aren’t many methods to implement a function; you can get started in about a year and become proficient in three years, while with C++, it can take three years just to understand its features. C can be used simply or complexly; C with class, small classes, and structures with pointers can easily be implemented. Look at the Linux kernel, the kernel header files, structures, macros, and various ingenious usages, and you’ll find that C++ is completely redundant. C++ dies from complexity. Experienced teams in large companies using C++ only use a subset of C++ features.

The many design features of C++ are not an advantage but a disadvantage. Don’t be fooled by the fact that it supports almost all features; too many choices actually mean no choice. A good language should have only one way to implement a function, like Python or Go.

In the application layer of products, the business logic is what matters. Various complex business logics can become chaotic with too many language features. C is completely sufficient, and various design patterns can also be implemented in C.

One can absorb some excellent features from the kernel, such as the kernel’s bidirectional linked list, some structures, macros, logging, memory management, thread management, inter-thread and inter-process communication, and various locks, which need to be encapsulated in C. Learning these things is essential to say you have mastered C. Even for beginners, having an advanced C programmer can guide a group of novices to write business code. However, an advanced C++ programmer cannot lead a group of beginners to complete the same project.

Resource constraints and efficiency limitations mean that for the same business function, C’s memory usage and speed are superior to C++. C++ has many ready-made solutions, but they are bulky and have many dependencies, making them too heavy for embedded environments. This means that for the same product, using C allows for lower-end main control chips and smaller memory, resulting in lower BOM costs compared to using C++. The competitive advantage of products using C is far greater than those using C++.

Answerer: Listening to the Sound of Heartbeats

The mainstream compilation language for microcontrollers will foreseeably remain a combination of C and a small amount of assembly for a long time. In the embedded Linux field, I believe the future leans towards a mixed programming paradigm with multiple languages, using C for kernel modules, and a mix of C++, Python, and Node.js for application layer logic, while the interface uses Java and QT/C++. Here are the reasons.

In the microcontroller field, C++ is not very popular due to historical reasons and industrial demand. Microcontrollers have evolved from the 51 architecture, and the mainstream flash capacity is still around 64KB to 256KB.

The current capacity limitations make it difficult to apply C++ features like templates, generic programming, and STL in development. However, if we do not use these and only use C++ that supports classes, there is no urgent need to switch from C to C++, especially when C has structures and function pointers that can serve as replacements. The promotion of Python and JavaScript faces similar challenges, compounded by debugging difficulties.

However, for Rust, this reasoning does not apply. Due to historical inertia, both large and small companies in the industry have a significant amount of legacy and ongoing C language projects (including original factory solutions). Replacing them with Rust is a commercial cost issue rather than a language issue (in my view, Rust is far superior to C at the language level). Therefore, Rust enthusiasts should focus on providing open-source projects for mainstream vendor platforms (specific projects, not just porting a hello world) rather than advocating for the superiority of syntax.

Moreover, the advantages of microcontrollers are not only real-time control but also low cost. For devices with production volumes in the tens of thousands or even millions, flash capacity is a significant cost. Therefore, the industry prefers to do the most with the least cost. From this perspective, C has a clear advantage over C++, Python, and JavaScript.

In the embedded Linux field, C++ is undoubtedly one of the main forces in the application layer. Although QT/C++ is gradually being replaced by Android/Java due to improved chip performance, it still occupies a mainstream position in fields such as medical, industrial control, and vehicle navigation. This is also one of the important application areas for C++. Saying that embedded systems are difficult and that C++ is also very challenging gives a very one-sided and objective impression of why embedded personnel learn C++ less.

Additionally, the difficult aspects of C++ include move semantics, template specialization, lambdas, and template metaprogramming. The various syntactic combinations of C++ can seem like a foreign language if one does not spend a lot of time studying them (with few exceptions). However, for the industry, especially in embedded applications, using STL to encapsulate vectors, maps, and algorithms for convenience, and encapsulating some template functions or classes for reuse, often means that many of C++11’s new features are not fully utilized. Saying it is difficult is somewhat exaggerated.

The industry’s challenge is always how to translate product requirements into specific task breakdowns (balancing performance, cost, and functionality while ensuring long-term stability), rather than which language to use to implement tasks. When demand drives any language, whether Python, JavaScript, C++, or Java, programming for salary means that as long as there is demand, someone will step into that direction. Difficulty is not the issue; demand and salary are the issues.

Answerer: idea4good

Let me start with the conclusion:

  1. C++ is very useful in embedded systems and microcontrollers;
  2. C with class is completely capable of handling most development tasks, and if used well, can significantly improve your code quality (in the embedded field, I personally do not encourage STL and templates, which I will discuss later).

GuiLite, with only 5,000 lines of code, is a commonly used GUI framework in embedded systems and microcontrollers. It is written in C++ and has 4.8K stars on GitHub and 2K stars on Gitee. You might wonder what can be done with 5,000 lines. It can not only handle regular interface elements but also perform 3D operations on microcontroller platforms, integrate with web pages to present interface effects in web form, and of course, support VR effects. Recently, it has also integrated with FFmpeg to support video playback without dependencies. There is no need for more words; the images speak for themselves:

Is C++ Becoming Widely Used in Embedded Software Programming?
Is C++ Becoming Widely Used in Embedded Software Programming?
Is C++ Becoming Widely Used in Embedded Software Programming?
Is C++ Becoming Widely Used in Embedded Software Programming?
Is C++ Becoming Widely Used in Embedded Software Programming?
Is C++ Becoming Widely Used in Embedded Software Programming?
Is C++ Becoming Widely Used in Embedded Software Programming?

This is not to say that GuiLite is exceptionally powerful, but to illustrate the charm of the C++ language. If C were used instead of C++, at least several thousand lines would be needed to achieve the same effect. Do you remember the famous Einstein bug equation? A little more code can significantly increase the number of bugs.

In fact, GuiLite is a typical example of C with Class. Many students might think this is very basic, but this is precisely the original intention behind the invention of C++. This feature allows you to completely say goodbye to function pointers; of course, many C experts have implemented all C++ features using function pointers.

First, kudos to the experts, but as ordinary developers, we must understand that the cost is a large number of function pointers. The presence of many function pointers greatly reduces code readability, while C with Class can elegantly eliminate all function pointers. Although you may think it is low-level, it can significantly reduce your code size. Moreover, it has excellent support from compilers; any microcontroller compiler can support this simple C++ feature.

If you have read the Linux virtual file system code, what repeatedly interrupts your understanding of the code? The answer is function pointers. To support multiple file systems, Linux uses function pointers extensively in the code. If inheritance and virtual functions were used instead, the code could be greatly simplified.

This is the cost of implementing inheritance and virtual functions in C. You might say: Linux’s method is efficient! The answer is: it doesn’t exist; no matter how you optimize at the C language level, it cannot compare with optimizations at the compiler level.

As developers, programming philosophy is far more important than syntactic sugar. C with Class represents a progress in programming philosophy, even though it is trivial in terms of syntax difficulty. Remember, I am talking about programming philosophy; even such simple syntax is still misused, completely disregarding actual needs, starting with a class without considering the original intention of class inventors.

Classes require you to restructure code at a high level, but you use them to pollute every detail, every line of code. Again, if used well, 5,000 lines can solve many problems; if used poorly, it is better not to use them at all. Just use the language you are most proficient in to pollute your code; that way, the pollution is more efficient, right?

Finally, are STL and templates suitable for embedded systems? Personally, I don’t think so. First, this poses a significant challenge to the compiler. Windows and Linux platforms are not an issue, but in microcontroller environments, compatibility may be a problem. Additionally, templates and STL are not very friendly for debugging, making them unsuitable for embedded and microcontroller development environments where the running cost (steps) is relatively complex.

The invention of STL and templates was not intended for embedded systems and microcontrollers. Therefore, forcing their use will bring you many troubles. The best use environment for STL and templates is large “games.” This set of tools is a typical product of trading space for time. Many impressive games require minimal CPU and memory resources, thanks to their contributions, but the cost is that your code will be relatively large. If you don’t have a 1TB hard drive, don’t play games~~~ Why can STL and templates thrive in the gaming industry?

First, the running efficiency is very high, and I won’t elaborate further. Secondly, the repetitiveness of games is too high. Recall that DOTA, League of Legends, and Honor of Kings are very similar in gameplay, right? It is precisely because of this high similarity that code reuse becomes very necessary; otherwise, the efficiency of industrializing games would be very low. The reason why large games can be released in just six months is due to STL and templates. Do you believe that? I say it is the credit of game engines. Do you believe that? I say game engines and STL/templates are intertwined; do you believe that?

In summary, the programming philosophy of C++ is very helpful for embedded developers, with the direct effect of significantly reducing your code size and logical complexity. However, the principles of STL and templates are not suitable for most embedded environments due to the special nature of embedded software, which often exceeds generality. The demand for code reuse is not strong, but as long as you understand why they were created, you will choose the appropriate environment for their use.

Source: Mindful Expert

-END-

Previous Recommendations: Click the image to jump to readIs C++ Becoming Widely Used in Embedded Software Programming?

Embedded development has low pay and many troubles; I advise you not to enter the industry?

Is C++ Becoming Widely Used in Embedded Software Programming?

Embedded development, learning microcontrollers has no future, often discouraged!

Is C++ Becoming Widely Used in Embedded Software Programming?

Rockchip’s chips, cost drastically reduced, computing power skyrocketing?

I am Lao Wen, an embedded engineer who loves learning.Follow me to become even better together!

Leave a Comment