![#ifdef __cplusplus extern "C" { #endif 的解释 #ifdef __cplusplus extern "C" { #endif 的解释](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
好多程序中都会遇到下列代码段:
#ifdef __cplusplus
extern “C” {
#endif
/****************** C语法代码段 ******************/
#ifdef_cplusplus
}
#endif /* __cpulsplus */
我们应该知道,__cplusplus是CPP中自定义的宏,表示这是一段CPP的代码,编译器按照C++的方式编译系统。
如果这时我们需要使用C语言的代码,那么就需要加上extern “C” {}这一段来说明,要不然编译器会把C代码按照C++模式编译;
#ifdef __cplusplus //c++编译环境中才会定义__cplusplus (plus就是"+"的意思)
extern"C"{ } //告诉编译器下面的函数是c语言函数(因为c++和c语言对函数的编译转换不一样,主要是c++中存在重载)
#endif