新项目中有如下代码
#ifdef __cplusplus
extern "C" {
#endif//快速获取唯一对象
OANetService * OANetServiceGet(void);
#ifdef __cplusplus
}
#endif
这两种关键字,都是为了实现C++与C兼容的,extern “C”是用来在C++程序中声明或定义一个C的符号,比如:
下面代码C++编译器会将在extern “C”的大括号内部的代码当做C语言来处理
extern “C” {
int func(int);
}
为了实现某个程序在C和C++中兼容,就有了__cplusplus,它是在C++中特有的,__cplusplus其实就是C++。
所以最上面的代码如果在C++ 文件中出现,经过编译,该段代码变成
extern "C" {
OANetService * OANetServiceGet(void);
}
如果在C文件中,经过条件编译,该段代码变成
OANetService * OANetServiceGet(void);












网友评论