我知道这不是一个好的项目开发方式,但由于我的工作原因,我需要在C项目中集成一些C ++数据结构(LRU缓存和哈希映射)。
到目前为止,我知道有一些方法可以使用extern "C"
调用C函数,但如何从C中调用C ++对象(方法...)呢?
我正在使用GCC。
如果所有的代码都使用C ++编译器编译,应该不会出现(或非常少)问题。
如果您使用gcc编译C并使用g ++编译C ++,则需要编写一个标题包装器,使C ++代码通过一组函数可见。
例:
MyClass.h
#ifdef __cplusplus
class MyClass
{
public:
MyClass() {/*STUFF*/}
~MyClass() {/*STUFF*/}
int doStuff(int x, float y) {/*STUFF*/}
};
extern "C" {
#endif
/* C Interface to MyClass */
void* createMyClass();
void destroyMyClass(void* mc);
int doStuffMyClass(void* mc, int x, float y);
#ifdef __cplusplus
}
#endif
源文件
MyClass.cpp
#include "MyClass.h"
void* createMyClass() {return reinterpret_cast<void*>(new MyClass);}
void destroyMyClass(void* mc) {delete reinterpret_cast<MyClass*>(mc);}
int doStuffMyClass(void* mc, int x, float y)
{
return reinterpret_cast<MyClass*>(mc)->doStuff(x,y);
}
#include "MyClass.h"
int main()
{
void* myClass = createMyClass();
int value = doStuffMyClass(myClass, 5, 6.0);
destroyMyClass(myClass);
}
在你的C++接口周围编写一个C包装器。将其编译为C ++,但请确保在extern "C"
块中包含您的C接口。这个新接口应该与您的C程序链接良好,并提供访问您的C ++代码的方法。
您需要创建与C兼容的转发函数,该函数将第一个参数作为指向对象的指针。转发函数通常会将第一个参数强制转换为正确的对象类型,并调用适当的成员函数。
// Function declaration in header
extern "C" void function(void *object, int param1, int param2);
// Function definition in source file
extern "C" function(void *object, int param1, int param2)
{
static_cast<Object*>(object)->member_function(param1, param2);
}
extern "C"
周围添加 #ifdef __cplusplus
,以便它们可以从 C 中使用,并且捕获可能出现在 C++ 代码中的任何异常( C 不支持异常...)。此外,与其使用 void*
来表示对象指针,不如预声明一个虚拟的 struct
,以实现最小化的类型安全。 - syam