extern "C"的作用:C++代码中,用 extern "C" 修饰的代码,会按照C代码编译。
解决的问题:C++支持函数重载,编译器编译函数的过程中会将函数的参数类型也加到编译后的代码中,而不仅仅是函数名;
C语言并不支持函数重载,因此编译C语言代码的函数时不会带上函数的参数类型,一般只包括函数名。
因此为了C++可以兼容调用C的代码,采用 extern "C"。
eg:
// 1.修饰整块代码
#ifdef __cpluscplus
extern "C" {
#endif
// some code
#ifdef __cplusplus
}
#endif
// 2.修饰一个方法
extern "C" int fun(int num);
// 3.修饰一个头文件
extern "C" {
#include "27.h"
}