Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


FreeRTOS's compatibility with C++

Posted by baoway on February 22, 2017


I've got a question regarding the compatibility between FreeRTOS and C++. FreeRTOS's kernel itself exist of C-files. A solution would be to work with extern "C" linkage in the C++-files and a global/ static task function, which receives a void* parameter in the main.cpp. This works for me so far. My general question is now: is it possible to write a FreeRTOS project completely as a C++-project or do I need the solution with extern "C" linkage above. Additionally is it possible to use the STL, especially the operators new and delete for allocating dynamic memory.

Any help would be greatly appreciated. Thanks, Bao

FreeRTOS's compatibility with C++

Posted by heinbali01 on February 22, 2017

FreeRTOS can be mixed perfectly with C++ code. Even the task-functions may have a C++ linkage, because xTaskCreate() only expects an address of a function.

Care should be taken with new() and delete(). I often redefine them as follows:

~~~~ void * operator new( size_t size ) { return pvPortMalloc( size ); }

void * operator new { return pvPortMalloc(size); }

void operator delete( void * ptr ) { vPortFree ( ptr ); }

void operator delete { vPortFree ( ptr ); } ~~~~

because the standard definitions will call malloc() and free().

Richard Damon created a wrapper for FreeRTOS, which you find here: https://github.com/richard-damon/FreeRTOScpp

FreeRTOS's compatibility with C++

Posted by richarddamon on February 23, 2017

Strictly speaking, the task function that FreeRTOS calls should be a function declaired as extern "C" as there may be a difference in calling conventions between "C" functions and "C++" functions. This does restrict (I believe) the task function to be a 'global' function and not a member of a class. In most cases, especially for the class of processors used with FreeRTOS, this isn't an issue, and you can use a static member function of the class for the task function. Currently my wrappers do this, but I am thinking of modifying them to be more strictly correct if I can work out all the corner cases.

For me, rather that trying to fix new/delete to use pvPortMalloc, I am more inclinde to replace malloc/free with a thread safe version. Newlib-nano has a version that can be made thread safe with just a compiler option and defining a couple of simple callback functions (and most gcc based systems will be using newlib-nano, but might not have that option set, so it isn't that hard to replace it. This says that if you use a C library routine that uses the heap you are still protected.

FreeRTOS's compatibility with C++

Posted by baoway on March 1, 2017

Hey Hein and Richard,

thank you both for the great answers. They definetely help a lot. I've already heard about the method of redefining the new() and delete() operators, so they would not call the standard malloc() and free() functions anymore, just like Hein mentioned above. I've added the Newlib-nano library to my linker, but I assume that the new() and delete() operators haven't been redefined. Richard, you have mentioned to define some callback functions, but how do they exactly look like?

Thank you in advance for helping, Bao

FreeRTOS's compatibility with C++

Posted by jwellbelove on February 2, 2018

I know it's nearly a year since the OP's post, but if you're interested in statically allocated STL like containers (no heap use at all) then you may be interested in the Embedded Template Library (ETL) It's open source under the MIT licence. https://www.etlcpp.com/

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.


FreeRTOS and other careers at AWS.

Latest News

FreeRTOS kernel V10.0.1 is available for immediate download. Now MIT licensed.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner