C++中的extern
这篇文章解释的简单明了:
https://stackoverflow.com/questions/10422034/when-to-use-extern-in-c
|
This comes in useful when you have global variables. You declare the existence of global variables in a header, so that each source file that includes the header knows about it, but you only need to “define” it once in one of your source files. To clarify, using extern int x; tells the compiler that an object of type int called x exists somewhere. It's not the compilers job to know where it exists, it just needs to know the type and name so it knows how to use it. Once all of the source files have been compiled, the linker will resolve all of the references of x to the one definition that it finds in one of the compiled source files. For it to work, the definition of the x variable needs to have what's called “external linkage”, which basically means that it needs to be declared outside of a function (at what's usually called “the file scope”) and without the static keyword. header: #ifndef HEADER_H #define HEADER_H // any source file that includes this will be able to use "global_x" extern int global_x; void print_global_x(); #endif source 1: #include "header.h" // it needs to be defined somewhere int global_x; int main() { //set global_x here: global_x = 5; print_global_x(); } source 2: #include <iostream> #include "header.h" void print_global_x() { //print global_x here: std::cout << global_x << std::endl; } |
C++中的extern的更多相关文章
- C++项目中的extern "C" {}
from:http://www.cnblogs.com/skynet/archive/2010/07/10/1774964.html C++项目中的extern "C" {} 20 ...
- OC中的extern,static,const
const的作用: const仅仅用来修饰右边的变量(基本数据变量p,指针变量*p). 被const修饰的变量是只读的. static的作用: 修饰局部变量: 1.延长局部变量的生命周期,程序结束才会 ...
- c与c++中的extern const的区别和联系
最近复习c++,发现了这个东西. c语言里面,我们在一个.c文件中用const定义了一个全局变量后,可以在另一个.c文件中用extern const来引用,但在c++中在链接的时候会报undefine ...
- c++中的 extern "C"(转载)
比如说你用C 开发了一个DLL 库,为了能够让C ++语言也能够调用你的DLL 输出(Export) 的函数,你需要用extern "C" 来强制编译器不要修改你的函数名. 通常, ...
- 【转载】c++中的 extern "C"(讲的更好一些)
[说明]本文章转载自 东边日出西边雨 的文章http://songpengfei.iteye.com/blog/1100239 ------------------------------------ ...
- 转:C++项目中的extern "C" {}
引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: #ifdef __cplusplus extern "C" { #endif /*...*/ #ifdef __c ...
- C和C++混合编程中的extern "C" {}
引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: 1 2 3 4 5 6 7 8 9 #ifdef __cplusplus extern "C" { #endif ...
- iOS中的extern与static
1.extern #import <Foundation/Foundation.h> extern NSString *DBDefaultName; @interface DataBase ...
- C语言中的 extern 关键字
今天在 BLE 中看到很多 extern 关键字,现在总结一下: extern 关键字主要用于在一个c文件中要用到另一个c文件中的变量或者函数. example: #extern_base.c ; # ...
- 嵌入在C++程序中的extern "C"
1.extern的作用 extern是C/C++语言中表明函数和全局变量作用范围(可见性)的关键字,可以告知编译器,用extern声明的函数和变量可以在本模块或其它模块中使用. 通常,在模块的头文件中 ...
随机推荐
- 对以内部 git 仓库为 composer 依赖的 package,加上版本号
现实问题 之前同事做了一个 composer package,做为公司大量 laravel 项目的通用模块. 但是,在实际使用中,每个项目对改 package 的依赖版本是有所不同的.否则 compo ...
- Fiddler抓包5-接口测试(Composer)
前言 Fiddler最大的优势在于抓包,我们大部分使用的功能也在抓包的功能上,fiddler做接口测试也是非常方便的. 对应没有接口测试文档的时候,可以直接抓完包后,copy请求参数,修改下就可以了. ...
- Paget Object 设计模式编写selenium测试用例
示例常规代码 baidu.py # _*_ coding:utf-8 _*_ import csv,unittest,time #导入csv模块 from time import sleep from ...
- 扩展方法 C#
“扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.” 定义和调用扩展方法 定义一个静态类以包含扩展方法. 该类必须对客户端代码可见. 有关可访问性规则 ...
- Could not find com.android.tools.build:aapt2:3.2.0-alpha14-4748712.
https://blog.csdn.net/lx6101989/article/details/80334232 android studio 升级到了3.0 取消了apt 报了这个错 在最上级的bu ...
- django中的null=true,blank=true,这个讲得清楚点
看mastering django:core,中文名<精通django>里的, 说得在理点. 截个图
- Web前端开发最佳实践(13):前端页面卡顿?可能是DOM操作惹的祸,你需要优化代码
文档对象模型(DOM)是一个独立于特定语言的应用程序接口.在浏览器中,DOM接口是以JavaScript语言实现的,通过JavaScript来操作浏览器页面中的元素,这使得DOM成为了JavaScri ...
- BZOJ1260 [CQOI2007]涂色paint 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1260 题意概括 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂 ...
- 【Java】 剑指offer(27) 二叉树的镜像
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 请完成一个函数,输入一个二叉树,该函数输出它的镜像. 思路 画图可 ...
- 031 分布式中,zookeeper的部署
一:准备 1.概述 为分布式应用提供协调服务的项目 提供一个简单的原语集合,以便于分布式应用可以在它之上构建更高层次的同步服务. 类似于文件系统那样的树形数据结构 目的:将分布式服务不再由于协作冲突而 ...