How does C++ perform in embedded?

How does C++ perform in embedded?

This is a hot topic in the field of zhihu embedded, the original link:

Several high praises answered: hmi panel module


Let’s start with the conclusion:

  1. Embedded, microcontroller inside C++ is very good to make;
  2. C with class for most of the development is completely competent, if used well, can significantly improve the quality of your code (embedded field, personally do not encourage STL and templates, this will be said later).

GuiLite, which has only 5,000 lines of code, is a GUI framework commonly used in embedded, microcontrollers; It’s written in C++, with 4.8K stars on GitHub and 2K stars on Gitee. Maybe what do you think 5,000 lines can do?

It can not only be a conventional interface element, but also 3D operation on the microcontroller platform, can be combined with the web page, the interface effect is expressed in the situation of the web page, of course, it also supports VR special effects, and recently integrated with FFmpeg, which can support video playback without dependence. It is useless to say more, as evidenced by the figure:

How does C++ perform in embedded?

This is not to say how strong GuiLite is, but to illustrate the charm of the C++ language, if you do not use the C++ language, and use C, it takes at least tens of thousands of lines to achieve the same effect; Remember the famous Einstein bug equation? With a little more code, the number of bugs increases significantly.

In fact, GuiLite is a typical C with Class; I believe that many students think that this is very low-level, but this is the original intention of the invention of the C++ language. This feature makes you completely say goodbye to function pointers; Of course, many C masters use function pointers to implement all the features of C++.

First of all, praise for the master, but as an ordinary leek, we must understand that its price is a lot of function pointers; As long as there is a large number of function pointers, the readability of the code is greatly reduced, and C with Class can eliminate all function pointers in the most elegant way, although you think it is very low, but it can make your code amount greatly reduced; And it has excellent support for compilers, and any microcontroller compiler can support this simple C++ feature.

If you’ve read Linux’s virtual file system code, what repeatedly interrupts you from grasping the meaning of the code? The answer is function pointers, and in order to achieve support for multiple file systems, Linus is desperately using function pointers in the code. If you choose to implement it with inheritance, virtual functions, its code can be greatly simplified.

This is the cost of implementing derived, virtual function extensions with C; You might say: Linus is very efficient this way! The answer is: not present; No matter how you optimize at the C level, it’s not comparable to compiler-level optimization.

As a developer, programming ideas are far more important than syntactic sugar. C with Class is an advance in programming thinking, although it is not worth mentioning in terms of syntactic difficulty. Remember, I’m talking about programming ideas here, even if it’s such a simple syntax, it’s still abused, completely out of consideration for practical needs, and it’s a class that doesn’t care about the original intent of the class inventor. The class requires you to reformulate the code at a high level, but you use it to pollute every detail, every line of code.

Or that sentence, use well, 5 thousand lines can solve a lot of problems; If you don’t use it well, it’s better not to use it, or use the language you’re best at to pollute your code, so that pollution is more efficient, right?

Finally, STL, are templates suitable for embedding? Personally feel that it is not very suitable, first of all, this is a great challenge to the compiler, Windows, Linux platform is not a problem, but there may be compatibility problems in the microcontroller environment; In addition, templates, STL are very unfriendly to debugging, and are not suitable for embedded, microcontroller development environments with relatively complex operating costs (steps).

STL, the original intention of the invention of the template is not for embedded, microcontrollers; Therefore, forcibly using it will bring you a lot of trouble. STL, the best environment for templates to use is for large “games”.

This set of things is a typical use of space for time products, a lot of awesome games required cpu, memory resources are very little, is their credit, but the price is that your code will be relatively large, there is no 1T hard disk, do not play the game ~ ~ ~ STL, why can the template in the game industry?

First of all, the operation efficiency is very high, and it will not be repeated here; Secondly, the repetitiveness of the game is too high, everyone recalls, DOTA, League of Legends, King of Glory in the gameplay is not very similar?

It is precisely because the similarity is too high, code reuse is very necessary, otherwise the efficiency of game industrialization is very low, and now the reason why a large game can be produced in half a year, I said this is the credit of STL, template, do you believe? I said it was the game engine that was the credit, do you believe it? I said game engine and STL, template is you have me, I have you, do you believe?

To sum up, C++ programming ideas are very helpful for embedded developers, and the direct effect is to greatly reduce your code volume and logic complexity; STL, template principle is not suitable for most embedded use environments, because the particularity of embedded software often exceeds the versatility, code reuse needs are not strong, but as long as you know why they are born, you will choose the appropriate use environment for them.

Listen to the sound of a heartbeat:

The mainstream compiled language of microcontrollers is foreseeably a combination of C and a small amount of assembly for the long term, while the future of embedded Linux seems to me to be more inclined to mixed application programming with multi-language paradigms, kernel modules using C, application layer logic using C++, Python, nodejs mixed programming, and interfaces using Java and QT/C++, here are the reasons.

In the field of microcontroller C++ is not very popular both historical reasons, but also the needs of the industrial world, for microcontrollers is from 51 to the present, the mainstream flash capacity is still around 64KB ~ 256KB, the current capacity limit is doomed to C++ in the template, generic programming and STL, etc. it is difficult to be applied to development, but if you do not use these, only use C++ that supports class, in the C language is a structure + function pointer can be replaced, from C to C++ There is no urgent need, and the difficulty of popularizing Python and JS has a similar reason, in addition to the difficulty of debugging.

However, for rust, this reason does not exist, but because of the inertia of history, at present, no matter how big or small the company in the industry, there are a large number of C language projects (including the original solution), replaced by rust is a business cost problem, not a language problem (in my opinion, rust language level is better than C too much), so rust lovers should be more for the mainstream vendor platform to provide open source projects (specific projects, not transplanting to run hello). World is done, being able to run and being able to use it in a product are two concepts), rather than calling for how good the grammar level is.

In addition, the advantage of single-chip microcomputer is not only real-time controllable, but the price is cheap, for shipments of hundreds of thousands or even millions of devices, flash capacity is also a considerable cost, so the industry prefers to do the most things with the smallest cost, in this regard, C is more than C++, Python, js has obvious advantages.

In the field of embedded Linux, C++ is definitely one of the main forces of the application layer, QT/C++ although it is gradually replaced by Android/Java because of the improvement of chip performance, but it still occupies a mainstream position in the fields of medical, industrial control, car navigation and other fields, and this is also one of the important application areas of C++, saying that embedded is more difficult, and C++ is also very difficult, so embedded personnel learn C++ less is a very one-sided and objective impression.

In addition, C++ is difficult to move semantics, template specialization, lambda, template metaprogramming and other knowledge, C++ various syntax combinations of strange tricks if not spend a lot of time to study, it looks like a book of heaven (few exceptions), but for the industry, especially for embedded class applications, only use STL encapsulated vector, map and algorithms to facilitate development, encapsulate some template functions or classes to help reuse, many times the new features of C++11 are not used, It would be a bit of an exaggeration to say that it was difficult.

The difficulty in industry is always how to convert the needs of the product into specific task decomposition (to meet the balance of performance, cost and function, while being able to achieve long-term stability), rather than what language to use to achieve the task, when the demand is oriented to any language, whether it is Python, js, C++ or Java, wage-oriented programming, as long as there is a demand, there will always be someone who will step into this direction, the difficulty is not the problem, the demand and salary are the problem.


The reality is that C++ is too hard, there are few embedded talents, and you have to be able to use C++ and not have moths, then even less.

So using C is really mainstream. Because the requirements for C programmers are still low.

I remember when I first engaged in embedded, the system did not even have an MMU, all the code of the whole system was in a memory space, and I had to manage the memory pool to avoid memory fragmentation. Any single memory access error can affect the code of a completely unrelated module. This system you dare to use C++?

Conclusion: If you’re developing code on your own and you’re confident in your C++ level, there’s certainly no problem with C++. But considering the C++ level and C language level of the overall programmer community, it is more realistic to use C for embedded projects.


As an embedded veteran for more than ten years, it can be said that CPP is too complex, there are too many language features, the implementation of a function can use dozens of methods, too many strange ways to achieve a function, CPP features are too complex to have more than 5 years of experience do not want to use well. However, the CPP ability of several people in a project group is inconsistent, and some strange features are used to achieve some functions, and there is no way to maintain it between multiple people.

In debugging, object-oriented debugging is best on the graphical interface of the tool is good debugging, and embedded most of the time there is no such debugging tool, CPP write business, post bug debugging will also kill you, CPP embedded debugging is more complex than C an order of magnitude.

Third, although the C language features are few, but completely enough, to achieve a functional method will not be many, about 1 year to start, 3 years veteran, and CPP 3 years even CPP features have not been figured out. C can be simple to use, can also be complex, C with class small cass, structs plus pointers easy to implement, look at the Linux kernel, look at the kernel header files, structs, macros and other subtle uses, you will find that CPP is completely redundant, CPP died of complexity. Experienced teams of large companies using CPP are all using a subset of CPP, using only a subset of the features.

Too many CPP design features are not advantages, but disadvantages, don’t look at what features are almost all supported, in fact, too many choices are actually no choice. Implementing a feature is a good language and only one way, such as Python, and go is not bad.

Fourth, the product application layer is actually important to the business, all kinds of complex business logic, too many language features will confuse the business logic. C is perfectly sufficient, various design patterns, C can also be achieved.

Can absorb some excellent features of the kernel, such as kernel two-way linked list, some structures, macros, logs, memory management, thread management, inter-thread process communication, all kinds of locks basically need C to encapsulate the sleeve to use, these things learned to say that the use of C. Even for novices, there will be no such advanced C usage, and having a high-level C can lead a group of low-level beginners to write business code. An advanced CPP can’t lead a bunch of beginner CPP beginners to the same project.

Fifth, resource limitations, efficiency limitations, the same business functions, C’s memory occupation, speed is higher than CPP, these things CPP are basically ready-made, but the size is large, dependent, too bulky for the embedded environment is too bulky. That is to say, the same product, the use of C can use a lower-end main control chip, smaller memory, product bom cost is lower than the use of CPP, product competitive advantage is much higher than the use of CPP.