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

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

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

跳至解决方案
3,330 次查看
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 项奖励
回复
1 解答
3,049 次查看
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 项奖励
回复
3 回复数
2,867 次查看
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 项奖励
回复
3,050 次查看
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 项奖励
回复
3,049 次查看
cavebiker
Contributor V

That was it! Name mangling.

Thanks a ton Martin

0 项奖励
回复