最近忙于进行跨组项目对接,于是遇到了一些小小的问题。比如,本项目中使用的开发语言为C语言,而其他组提供的接口均使用c++语言,那么就会出现跨语言的问题。接下来将实现一个小小的demo,用于分装c++中的类,并提供C语言使用。该文章主要用于帮助初学者以及日后自我回顾。
【c++接口】
//test_a.cpp
#include "test_a.h"
test_a::test_a():_a(1),_b(2)
{
}
int test_a::get_A()
{
return _a;
}
int test_a::get_B()
{
return _b;
}
//test_a.h
class test_a{
public:
test_a();
int get_A();
int get_B();
private:
int _a;
int _b;
};
【wrapper】
//wrapper.h
struct tagtest;
#ifdef __cplusplus
extern "C" {
#endif
struct tagtest* getInstance();
int getTestA(struct tagtest* pinstance);
int getTestB(struct tagtest* pinstance);
#ifdef __cplusplus
}
#endif
//wrapper.cpp
#include <iostream>
#include "wrapper.h"
#include "test_a.h"
struct tagtest {
test_a test;
};
struct tagtest* getInstance()
{
return new struct tagtest;
}
int getTestA(st