FreeRTOS, C++ and xTaskCreate() function

anonymous wrote on Sunday, November 03, 2013:

Hi!

I try to create a C++ application (and C++ tasks) based on FreeRTOS but I can not call the xTaskCreate() function from my C++ code.

Default FreeRTOS main() function:

/* default FreeRTOS main() */

int main( void )
{
    prvSetupHardware();
    /* Exported C++ function */
    runTask();
    ...
}

Below the header and source files based on the following both examples:

http://sourceforge.net/p/freertos/discussion/382005/thread/5d5201c0
http://interactive.freertos.org/entries/223648-using-freertos-with-c

I think I have included all relevant C-header files in my C++ header:

#ifdef __cplusplus
extern "C" {
#endif
#include <FreeRTOS.h>
#include <task.h>
#ifdef __cplusplus    
}
#endif

… and my C/C++ wrapper functions:

#ifdef __cplusplus
extern "C" {
#endif
void tskWrp(void* p);
void runTask();
#ifdef __cplusplus
}
#endif

C++ source file: prl_task.cc

/* C++ source file: prl_task.cc /*   

/*-----------------------------*/
/* 2 simple tasks */

void vTask1( void ) {
    doSomething();
}

void vTask2( void ) {
    doSomething();
}

/*------------------------------*/
/* base class CTask */

CTask::CTask(char const* name,
	unsigned portBASE_TYPE priority,
	unsigned portSHORT stackDepth=1000) {
    
    /* Code works up to here! */    
    //vTask1();
    xTaskCreate(&tskWrp, (signed char*)name, 1000, this, 1, NULL);
}

void CTask::Task() {

 vTask1();
}
/*-------------------------------*/
/* derived class CTask1 */

CTask1::CTask1(char const* name,
            unsigned portBASE_TYPE priority,
	unsigned portSHORT stackDepth=1000) : CTask(name, priority,stackDepth) {
}

void CTask1::Task() {

 vTask2();
}

/*--------------------------------*/ 
/* interface functions C C++ */

void runTask(){

    CTask1 task("tsk",1,1000);
}

/*--------------------------------*/
/* wrapper function */

void tskWrp(void* p) {

(static_cast<CTask*>(p))->Task();
}

I can compile the code without errors but the xTaskCreate() function will be “ignored” and the program is not running on my MPU. If I call directly another functions e.g. vTask1() instead of xTaskCreate() the program works fine on the MPU.

By the way my IDE throws a warning “Function ‘xTaskGenericCreate’ could not be resolved”. I’m not sure whether the IDE warning or any other reason is the cause of my problem.

Maybe someone can help me?

Florian

richard_damon wrote on Sunday, November 03, 2013:

First, you shouldn’t need the #ifdef __cplusplus lines to add the extern “C” stuff, as that should be in the headers already.

The other thing to note it that the FreeRTOS files MUST be compelled a C files, not C++ files, or they won’t work right, The extern “C” stuff in the headers sets up the linkage properly for the cross language link. That might explain the could not be resolved problem.

anonymous wrote on Sunday, November 03, 2013:

If I will create a C-compiled freeRTOS.o object file and a C++ compiled application.o object file, is it possible to link both together by using g++?

The following link recommends to use g++ for the linking process in case ob mixing C and C++:

http://www.parashift.com/c++-faq/overview-mixing-langs.html

richard_damon wrote on Sunday, November 03, 2013:

Yes, you should be able to link files compiled as C and files compiled as C++, that is the main purpose of the extern “C” syntax. As the link suggests, main should be C++, the libraries used should be C++, so the linker should generally be the C++ linker. The C compiler needs to be ABI complient with the C++ compiler.