Using FreeRTOS with C++

FreeRTOS while it currently can not be complied as a C++ file (due, in part, to the way data hiding is implemented, and how it uses void* pointers) is generally compatible with C++ programs.  The header files include the needed extern "C" { } blocks to enable the C to C++ linkage rules. When using FreeRTOS in a C++ program there are a few things to remember:

Task functions need to be declared as extern "C" void taskfun(void *parm) to make sure the function is compiled with C linkage calling conventions and has the proper parameters. On some platforms the extern "C" may not actually be needed as C++ and C functions use the same calling conventions, but it is still best to do so. taskfun may be a global function, or a static member function, but may not be a normal member function, because these have an extra parameter (this) that needs to be sent to the function. If you wish to use a member function, then you need to use a wrapper function as follows

class myclass {


    void taskfun();

} obj;

extern "C" void taskfunwrapper(void* parm) {



then you can create the task as:

xTaskHandle tcb;

xTaskCreate(&taskfunwrapper, "taskname", stackDepth, &obj, prioriry, &tcb);

The other major limitation is that when you use a queue to transfer data, the data being sent should really be a POD (Plain ol Data) or at very least, needs to have trivial assignment operator as the queue will over the data in and out of the queue with a plain memcopy operation, not the assignment operation.


Please sign in to leave a comment.