Quality RTOS & Embedded Software

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


Loading

FreeRTOS, C++ and xTaskCreate() function

Posted by lagavulin21 on November 3, 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


FreeRTOS, C++ and xTaskCreate() function

Posted by richard_damon on November 3, 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.


FreeRTOS, C++ and xTaskCreate() function

Posted by lagavulin21 on November 3, 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


FreeRTOS, C++ and xTaskCreate() function

Posted by richard_damon on November 3, 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.


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


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

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



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

Renesas

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