extern 用法
1 修饰函数,用C函数的方式编译;方便c++代码调用该函数
#ifdef __cplusplus extern "C" { #endif Interface* create(); #ifdef __cplusplus } #endif ..... #ifdef __cplusplus extern "C" { #endif Interface* create() { return new Interface(); } #ifdef __cplusplus } #endif
2 声明函数或者变量,在其他文件中定义; extern int g_a = 1 在声明时定义,编译会重复声明错误
3 static 和 extern 不能同时使用。static修饰的全局变量声明与定义同时进行,只作用于本身编译单元。
4 const 和 extern, const作用于本编译模块,两个一起用可以作用于其他模块。
extern const char g_str[]; // 声明
const char g_str[] = "123456"; // 定义