Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


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

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

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists