Why can't C++ replace C in embedded systems?

Author Topic: Why can't C++ replace C in embedded systems?  (Read 814 times)

Offline momin.ce

  • Newbie
  • *
  • Posts: 28
  • Today is a gift. Use it properly.
    • View Profile
    • Khondhaker Al Momin
Why can't C++ replace C in embedded systems?
« on: February 24, 2020, 01:04:03 PM »
C++ can be used in embedded systems. If it can replace C - that would be a longer discussion (I mean moving an existing project from C to C++).

There is a lot of myths about C++, that mostly untrue. However, very experienced C programmers from Embedded World are rarely good C++ programmers without proper training and time to exercise new language. Mainly, because one myth is that C is a subset of C++ or that C++ is a superset of C. It’s true historically, but today both languages differ a lot and you cannot apply the way of solving problems know from C into your C++ programs as otherwise, results are disastrous. And that’s what very experienced C programmers are trying to do starting with C++ language - the project ends up with code written in C with classes style.

Actually C++ has multiple features that make code on the embedded system smaller and faster than it would be written in C. Massive optimisations offered by compilers help a lot as well. It’s much easier to write correct code in C++ and it’s much easier to unit test C++ code before you use it on your target system.

But preparation to run C++ code on the target can be pretty painful and you need people who really know what they’re doing. All your toolchain need to be configured accordingly and widely available defaults are fine only to target Linux based systems, but not bare metal, standard libraries have their quirks that need to be sorted, otherwise, you face equivocal problems during execution. But once that’s done, you’re good to go.

One another problem is that support for C++ from well recognized Real-Time Operating Systems is close to zero - it requires some time to get is sorted and create a proper layer between the OS written in C and project abstractions for the RTOS. None of the attempts to make fully-fledged RTOS in C++ really went into mainstream. And today multithreading support of C++14 and even extended in C++17 makes it even more complex. Somebody well addressing this problem today may finally patch the hole in the growing IoT market that is more and more often implemented using C++.

I forgot to mention in the original answer, that the obstacle for many who would like to start their adventure with C++ in embedded systems are compilers. If you can afford to use GCC, then you’re mostly fine or ARM (not all) and MIPS, but other architectures are supported only by commercial products. And for example, IAR added support for C++14 quite recently in their Workbench compilers, while the current approved standard is C++17 and all-new GCC and CLANG support it.

I’m currently in the project, where we use C++14 on target platform having 2MB of code space (built-in Flash) and 364 kB of RAM. The processor core in ARM Cortex-M7. We’re using FreeRTOS (in fact licensed OpenRTOS). It has HTTP Server with REST API and AMQP for cloud communication. Networking over WiFi.

Source: Michał Fita
Programmer, electronics hobbyst,
Department of Civil Engineering,
Daffodil International Unversity.