
一 在C源文件中调用C++封装的接口
例如:
要想在A.c文件中,调用生命在B.h,实现在B.cpp中的接口bool getMAC(char *mac_addr);
其实现方法 B.cpp 如下:
// B.cpp #ifndef _cplusplus
#define _cplusplus
#endif #include <stdio.h> bool getMAC(char *mac_addr)
{
// your code }
B.h 头文件的声明为:
// B.h #ifndef _B_H
#define _B_H #ifdef __cplusplus //__cplusplus是cpp中自定义的一个宏
extern "C" { //告诉编译器,这部分代码按C语言的格式进行编译,而不是C++的
#endif bool getMAC(char *mac_addr); #ifdef __cplusplus
}
#endif #endif
A.c 中正常调用即可
// A.c #include "B.h"
#include <stdio.h> int main()
{
bool bRet = false;
char chMac[] = {}; bRet = getMAC(chMac); return ;
}