Linking .cpp & .c _tasks using MQX4.0 CW10.3

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Linking .cpp & .c _tasks using MQX4.0 CW10.3

Jump to solution
2,209 Views
cavebiker
Contributor V

I'm trying to add a C++ task into a C project, the example cplus.cpp code. cplus.cpp builds and runs fine on my custom BSP alone. I moved it into my main project and tried to fire off the cplus_task() from inside my C code, like all my other _tasks but the linker complains it cannot find cplus_task(). 

Does anyone know if I can do this?

Or is there another way?

Thanks much for any help or ideas, this may be my last big hurdle (for awhile of course).

Cheers,

Tom

0 Kudos
1 Solution
1,928 Views
Martin_
NXP Employee
NXP Employee

Hi Tom,

C++ allows overload function names. We can have a function void dummy(void) as well as void dummy(int)
with the only difference being the parameters.

Thus, for proper linkage, the linker needs to know which from the two functions it should link.
The C++ compiler uses name mangling, it adds list of parameters into the function names, for example:

void __dummy_v (void);
void __dummy_i (int);

If you're in a *.c module and you try to call cplus_task defined in a *.cpp module, then due to C++ name mangling the extern function is not "cplus_task" but something like "__cplus_task_ui" or something like that. You should be able to see the name in disassemble listing of cplus.cpp.


What you can do is to have the TASK_TEMPLATE_STRUCT definition in a *.cpp file and refer to all C tasks via extern "C" {} keyword. That's exactly how you would use MQX API from a C++ source file.

View solution in original post

0 Kudos
3 Replies
1,746 Views
misterbee
Contributor II

I have the same issue, but not sure to understand the solution here.

Hi @AMAR ,

Could you elaborate what you meant here:

quote:

"What you can do is to have the TASK_TEMPLATE_STRUCT definition .."

How to do that?

0 Kudos
1,929 Views
Martin_
NXP Employee
NXP Employee

Hi Tom,

C++ allows overload function names. We can have a function void dummy(void) as well as void dummy(int)
with the only difference being the parameters.

Thus, for proper linkage, the linker needs to know which from the two functions it should link.
The C++ compiler uses name mangling, it adds list of parameters into the function names, for example:

void __dummy_v (void);
void __dummy_i (int);

If you're in a *.c module and you try to call cplus_task defined in a *.cpp module, then due to C++ name mangling the extern function is not "cplus_task" but something like "__cplus_task_ui" or something like that. You should be able to see the name in disassemble listing of cplus.cpp.


What you can do is to have the TASK_TEMPLATE_STRUCT definition in a *.cpp file and refer to all C tasks via extern "C" {} keyword. That's exactly how you would use MQX API from a C++ source file.

0 Kudos
1,928 Views
cavebiker
Contributor V

That was it! Name mangling.

Thanks a ton Martin

0 Kudos