码迷,mamicode.com
首页 > 其他好文 > 详细

extern 用法

时间:2018-06-23 11:37:17      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:code   style   color   ++   col   全局变量   函数   creat   str   

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";  // 定义

extern 用法

标签:code   style   color   ++   col   全局变量   函数   creat   str   

原文地址:https://www.cnblogs.com/bzadhere/p/9216596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!