【C++ 拾遗】extern 关键字
Separate compilation
let us split our programs into several files, each of which can be compiled independently.
allows programs to be written in logical parts.
Distinction between a declaration and a definition
To support separate compilation, C++ distinguishes between declarations and definitions. A declaration makes a name known to the program. A definition creates the associated entity.
A variable declaration specifies the type and name of a variable. A variable definition is a declaration. In addition to specifying the name and type, a definition also allocates storage and may provide the variable with an initial value.
One Definition Rule
Variables must be defined exactly once but can be declared many times.
Where extern comes in
When we separate a program into multiple files, we need a way to share code across those files. For example, code written in one file may need to use a variable defined in another file. A file that wants to use a name defined elsewhere includes a declaration for that name.
To obtain a declaration that is not also a definition, we add the extern keyword and must not provide an explicit initializer:
extern int i; // declares but does not define i;
int j; // declares and defines j
In this case, the extern signifies the the variable i is not local to this file and that its definition will occur elsewhere.
Any declaration that includes an explicit initializer is a definition. We can provide an initializer on a variable defined as extern, but doing so overrides the extern. An extern that has an initializer is a definition.
It is an error to provide an initializer on an extern inside a function.
extern 的第二个用法
To define a single instance of a const variable, we use the keyword extern on both its definition and declaration(s). In other words, to share a const object among multiple files, you must define the variable as extern.
extern 的第三个用法
explicit instantiation a.k.a. extern template
【C++ 拾遗】extern 关键字的更多相关文章
- 16-static和extern关键字2-对变量的作用
上一讲介绍了static和extern对函数的作用,static用来定义一个内部函数,不允许其他文件访问:extern用来定义和声明一个外部函数,允许其他文件访问.static和extern对变量也有 ...
- 15-static和extern关键字1-对函数的作用
一.extern与函数 如果一个程序中有多个源文件(.c),编译成功会生成对应的多个目标文件(.obj),这些目标文件还不能单独运行,因为这些目标文件之间可能会有关联,比如a.obj可能会调用c.ob ...
- 【转载】C/C++中extern关键字详解
1 基本解释:extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义.此外extern也可用来进行链接指定. 也就是说extern ...
- C++中extern关键字用法小结
总结C++中关于extern关键字的用法. 1.变量的生明和定义中 C++语言支持分离式编译机制,该机制允许将程序分割为若干个文件,每个文件可被独立编译.为了将程序分为许多文件,则需要在文件中共享代码 ...
- 解决全局变量共享---C语言的extern关键字用法
在调试程序时,有一个参数需要在多个函数之间传递,因为是作为调试参数,不想将参数引入到函数中. 很自然的想到使用全局变量来表示这个公共参数,工程代码的结构如下: main.c test.c test.h ...
- C语言中的 extern 关键字
今天在 BLE 中看到很多 extern 关键字,现在总结一下: extern 关键字主要用于在一个c文件中要用到另一个c文件中的变量或者函数. example: #extern_base.c ; # ...
- 转载 浅谈C/C++中的static和extern关键字
浅谈C/C++中的static和extern关键字 2011-04-21 16:57 海子 博客园 字号:T | T static是C++中常用的修饰符,它被用来控制变量的存贮方式和可见性.ext ...
- 浅谈C/C++中的static和extern关键字 转
原文:http://developer.51cto.com/art/201104/256820.htm static是C++中常用的修饰符,它被用来控制变量的存贮方式和可见性.extern, &quo ...
- 关于全局变量和函数,在其他类中调用问题,extern关键字
1个工程下有几个类文件,和1个全局的文件comm.h, comm.h中存放了这几个类同时需要的变量和同时调用的函数. 当时,我只在comm.h中定义: int commData1: vector&l ...
- C语言的static和extern关键字
我的博客:www.while0.com 如果A.c要包含B.c里的一个变量或函数,则在A.c中要用extern关键字声明.注意: ①如果是包含的B.c里的函数,则在A.c里声明的时候可以不写exter ...
随机推荐
- linux下进程的最大线程数、进程最大数、进程打开的文件数
linux下进程的最大线程数.进程最大数.进程打开的文件数 ===========最大线程数============== linux 系统中单个进程的最大线程数有其最大的限制 PTHREAD_TH ...
- 谈谈toLocaleString()
如何理解toLocaleString()? toLocaleString()就是把数组转换为本地字符串.首先调用每个数组元素的toLocaleString()方法,然后使用地区特定的分隔符把生成的字符 ...
- node 写api几个简单的问题
最近出了一直在做无聊的管理后台,还抽空做了我公司的计费终端,前端vue,后端node,代码层面没啥太多的东西.由于自己node版本是8.0.0,node自身是不支持import和export的,要想基 ...
- JDK9 新特性
JDK9 新特性目录导航 目录结构 模块化系统 jshell 多版本兼容JAR 接口的私有方法 改进try-with-resourcs 改进砖石操作符 限制使用单独下划线标识符 String存储结构变 ...
- SHOPEX快递单号查询插件圆通V8.2专版
SHOPEX快递物流单号查询插件特色 本SHOPEX快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世 ...
- wamp环境下安装imagick扩展
先上图,如下是安装成功后的phpinfo()界面: 安装步骤: 1.先确定安装版本,比如我的的php : php7.0.12 x86 ts 那么就需要三方版本 要一致:imagick软件本身( 如x ...
- Mysql基础1-基础语法-字段类型
主要: 基础 字段类型 基础 基本概念 1) 数据库分类 层次数据库,网状数据库,关系数据库 常见:SQL Server, Oracle,infomix,sybase,ibmDB2,Mysql 2)数 ...
- 使用MapReduce读取HBase数据存储到MySQL
Mapper读取HBase数据 package MapReduce; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hba ...
- python 中 pynlpir错误 Cannot Open Configure file pynlpir\Data\Configure.xml 解决
在用python做分词.数据处理的时候,想调用pynlpir库,pynlpir.open()时出现错误,更新一下授权文件还是错误, 仔细一看错误是:Cannot Open Configure file ...
- Cadence17.2下载ALTERA的FPGA封装库
1. 在Cadence的安装目录里面找了下,发现都没有Altera的FPGA型号的函数库,下面的虽然是ALTERA,但是没有FPGA的器件封装 2. 去intel的官网看能不能下载到,INTEL网址, ...