How can I call a C++ function from a C program, is it possible?, and if it is how can I do it?. Thank you.
如何从C程序调用C ++函数,是否可能?,如果是,我该怎么办呢?谢谢。
1 个解决方案
#1
If you are trying to call a C++ function from C, then you are probably running into name mangling issues. The compiler does this in order to support function overloading and other features of C++.
如果你试图从C调用C ++函数,那么你可能会遇到名称错误问题。编译器执行此操作是为了支持函数重载和C ++的其他功能。
You can use extern "C"
to inform the C++ compiler that the function CMACInit()
will be called from C code:
您可以使用extern“C”通知C ++编译器将从C代码调用函数CMACInit():
extern "C" CMACInit() { ... }
When declared in this way, the C++ compiler will not mangle the name and will set everything up so the function can be called from C code.
当以这种方式声明时,C ++编译器不会破坏名称并将设置所有内容,以便可以从C代码调用该函数。
#1
If you are trying to call a C++ function from C, then you are probably running into name mangling issues. The compiler does this in order to support function overloading and other features of C++.
如果你试图从C调用C ++函数,那么你可能会遇到名称错误问题。编译器执行此操作是为了支持函数重载和C ++的其他功能。
You can use extern "C"
to inform the C++ compiler that the function CMACInit()
will be called from C code:
您可以使用extern“C”通知C ++编译器将从C代码调用函数CMACInit():
extern "C" CMACInit() { ... }
When declared in this way, the C++ compiler will not mangle the name and will set everything up so the function can be called from C code.
当以这种方式声明时,C ++编译器不会破坏名称并将设置所有内容,以便可以从C代码调用该函数。