【嵌入式】keil5中使用C,C++混合编译

发布时间 2023-07-05 20:07:44作者: 海底淤泥

5份文件:a.c和a.h和b.cpp和b.h和main.c

其中main.c调用b.cpp,b.cpp调用a.c

 

main.c

#include "./b.h"

int main(void)
{
	int t=funC();
	while (1)
	{
	}
}

 

b.h

#ifndef _B_
#define _B_
#ifdef __cplusplus
extern "C" {
#endif
int funC(void);
#ifdef __cplusplus
}
#endif
#endif

 b.cpp

#include "./b.h"
#include "./a.h"
class B{
	public:
            int funB(void);
};
int B::funB(void){
	return funA();
}
#ifdef __cplusplus
extern "C" {
#endif	
int funC(void){
	B* b=new B();
	return b->funB();
}
#ifdef __cplusplus
}
#endif

 

a.h

#ifndef _A_
#define _A_
#ifdef __cplusplus
extern "C" {
#endif
int funA(void);
#ifdef __cplusplus
}
#endif
#endif

a.c

#include "./a.h"
 
int funA(){
	return 2;
}

 

参考:https://blog.csdn.net/weixin_30241919/article/details/99173820