Embedded SW uses STL or not
As the complexity increasing of embedded software, more and more projects/products use C++ as the implementation language. Firstly, I want to make a conclusion which applied in our company.
- Using C++ for a stronger MCU like 32bit MCU like panel, gateway, etc.
- Using C for 8bit MCU mostly like detector, input module, etc.
We can use the strong features of C++ to implement for time and cost saving, like object-oriented design, encapsulate, polymorphic, template, libraries, design pattern, etc. but some features, we shall be careful, for example STL. Also conclusion listed here.
- For Linux OS used system, we can use STL freely.
- For RTOS, we deny to use STL.
The considerations of using STL or not are the memory management, code size, performance, etc. I can list the cons below.
- Dynamic memory allocation are used
- STL allocators are slow, bloated, low performance
- Inline function increase the code size, not all compiler do the optimization very well
As we known, the No.1 rule of embedded development special for real time embedded is allocating memory “in-front”.
In conclusion, the C++ features shall be applied judiciously. Whether or not, depend on the balance of cost/effort, experience, etc.
By the way, why not use C++ to write the Linux Kernel? What’s the answer from Linus Torvalds: writing kernel code in C++ is a BLOODY STUPID IDEA. The fact is, C++ compilers are not trustworthy. The whole C++ exception handling thing is fundamentally broken. It's _especially_ broken for kernels. Any compiler or language that likes to hide things like memory allocations behind your back just isn't a good choice for a kernel.” (quoted in the linux-kernel FAQ)
浙公网安备 33010602011771号