# FreeRTOS, C++ and xTaskCreate() function

**URL:** <https://forums.freertos.org/t/freertos-c-and-xtaskcreate-function/4961>\
**Category:** Kernel\
**Created:** [November 3, 2013, 6:13pm UTC](https://forums.freertos.org/t/freertos-c-and-xtaskcreate-function/4961 "2013-11-03T18:13:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 3, 2013, 6:13pm UTC](https://forums.freertos.org/t/freertos-c-and-xtaskcreate-function/4961/1 "2013-11-03T18:13:10Z")

</div>

**anonymous** wrote on [Sunday, November 03, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/da582460/#e10f):

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://sourceforge.net/p/freertos/discussion/382005/thread/5d5201c0)  
[http://interactive.freertos.org/entries/223648-using-freertos-with-c](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

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [November 3, 2013, 7:08pm UTC](https://forums.freertos.org/t/freertos-c-and-xtaskcreate-function/4961/2 "2013-11-03T19:08:20Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Sunday, November 03, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/da582460/#251f):

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.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 3, 2013, 8:30pm UTC](https://forums.freertos.org/t/freertos-c-and-xtaskcreate-function/4961/3 "2013-11-03T20:30:25Z")

</div>

**anonymous** wrote on [Sunday, November 03, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/da582460/#5d5d):

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](http://www.parashift.com/c++-faq/overview-mixing-langs.html)

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [November 3, 2013, 8:53pm UTC](https://forums.freertos.org/t/freertos-c-and-xtaskcreate-function/4961/4 "2013-11-03T20:53:59Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Sunday, November 03, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/da582460/#18a8):

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.
