FreeRTOS support for C++ Templates and STL (I.e. Standard Template Library)?

Hi Team,

I have following questions about FreeRTOS support:

  1. Does FreeRTOS support C++ Templates? (for generic programming to create templatized classes methods etc.)
  2. Does FreeRTOS support Standard Template Library (STL). So that we can create queues, lists arrays etc.

Any inputs on this will be very helpful.

Thanks & Regards,

  • Ajit Bhor

You‘re free to use whatever your compiler supports.
FreeRTOS is just an OS which shouldn’t and doesn’t restrict how application code is written. FreeRTOS code itself already takes care about getting seamlessly mixed with C++ application code by exporting its API as extern “C”.
For instance I’m using C++ including STL for my FreeRTOS applications.
The only maybe non-obvious thing you have to take care about is the (thread-safe) heap needed for new/delete operators.
The standard heap implementation (malloc, free, realloc) is usually part of the C standard library (e.g. newlib) which is part of the compiler tool chain. But it requires system support through the _sbrk system call which needs to be implemented. Often there is a syscalls.c file which contains implementations or wrappers for all possible system calls used by the C standard library.
Alternatively one of the provided (optional) FreeRTOS heap implementations can be used for your application code, too.
In this case you have to override the C library heap interface (malloc,free, realloc) with corresponding wrapper functions.
There are some good resources in the net like Dave Nadler’s article worth reading.

FreeRTOS includes the extern “C” statements needed to include its headers from C++ code.
Other than that, it doesn’t do much to provide direct support for C++, nor does it do anything to prevent the use of C++.

I have written a simple set of C++ wrappers for FreeRTOS (https://github.com/richard-damon/FreeRTOScpp) That does some of the things you mention.

One big thing to note, FreeRTOS Queues and Streams do NOT support full classes for their data, but only POD (Plain Ol Data) objects, because they are moved with memcpy, not the C++ copy constructors or assignment operators.

Years ago, I tried to port just std::string and std::vector from the STL, but very quickly blew the amount of RAM available to me in my embedded project, and gave up. A clone of a few key templates that was free of the stream I/O dependencies might work. But now I’m using Cypress PSoC Creator, which makes it very difficult to use anything besides straight C (which is a shame).

EDIT: Not “RAM,” but flash. The code size was too big.

Thanks @hs2 Helmut. @FreeRTOSSupport Ajit Bhor, many of us have been using C++ including templates on FreeRTOS for donkeys years. You need to be very careful about memory management if you are using it (and you generally will with STL). STL isn’t really a current term, but I think I know what you mean? Also, beware of some bugs like this one: Bug #1905459 “Not thread-safe: C++ Exception Processing Crashes ...” : Bugs : GNU Arm Embedded Toolchain (please note to ARM if this also affects you!).
Hope that helps!
Best Regards, Dave