这篇文章解释的简单明了:

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的更多相关文章

  1. C++项目中的extern "C" {}

    from:http://www.cnblogs.com/skynet/archive/2010/07/10/1774964.html C++项目中的extern "C" {} 20 ...

  2. OC中的extern,static,const

    const的作用: const仅仅用来修饰右边的变量(基本数据变量p,指针变量*p). 被const修饰的变量是只读的. static的作用: 修饰局部变量: 1.延长局部变量的生命周期,程序结束才会 ...

  3. c与c++中的extern const的区别和联系

    最近复习c++,发现了这个东西. c语言里面,我们在一个.c文件中用const定义了一个全局变量后,可以在另一个.c文件中用extern const来引用,但在c++中在链接的时候会报undefine ...

  4. c++中的 extern "C"(转载)

    比如说你用C 开发了一个DLL 库,为了能够让C ++语言也能够调用你的DLL 输出(Export) 的函数,你需要用extern "C" 来强制编译器不要修改你的函数名. 通常, ...

  5. 【转载】c++中的 extern "C"(讲的更好一些)

    [说明]本文章转载自 东边日出西边雨 的文章http://songpengfei.iteye.com/blog/1100239 ------------------------------------ ...

  6. 转:C++项目中的extern "C" {}

    引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: #ifdef __cplusplus extern "C" { #endif /*...*/ #ifdef __c ...

  7. C和C++混合编程中的extern "C" {}

    引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: 1 2 3 4 5 6 7 8 9 #ifdef __cplusplus extern "C" { #endif ...

  8. iOS中的extern与static

    1.extern #import <Foundation/Foundation.h> extern NSString *DBDefaultName; @interface DataBase ...

  9. C语言中的 extern 关键字

    今天在 BLE 中看到很多 extern 关键字,现在总结一下: extern 关键字主要用于在一个c文件中要用到另一个c文件中的变量或者函数. example: #extern_base.c ; # ...

  10. 嵌入在C++程序中的extern "C"

    1.extern的作用 extern是C/C++语言中表明函数和全局变量作用范围(可见性)的关键字,可以告知编译器,用extern声明的函数和变量可以在本模块或其它模块中使用. 通常,在模块的头文件中 ...

随机推荐

  1. Lucene.Net简介

    说明:Lucene.Net 只是一个全文检索开发包 .查询数据的时候从Lucene.Net查询数据.可以看做是一个提供了全文检索功能的数据库. 注意:只能搜索文本字符串. 重要概念:分词,基于词库的分 ...

  2. linux下如何使用gdb调试

    gdb是linux下非常好用的一个调试工具,虽然它是命令行模式的调试工具,但是它的功能强大到你无法想象,这里简单介绍下gdb下常用的命令. 首先编译生成可执行文件(这里的test.c是一个简单的求前n ...

  3. DDD领域模型数据访问之对象(十一)

    在工程DDD.Domain中文件夹ModelPermission新建类:BAS_Object public partial class BAS_Obejct:AggreateRoot { //仓储接口 ...

  4. ATL CAxWindow类创建问题一则

    查看一个浏览器源码实现,发现其中使用了ie的控件,但例子中没有找到任何创建ie浏览器控件的代码,经过仔细跟踪,发现CAxWindow类是可以这么使用滴.. 创建的时候第三个参数直接传入url.调用到C ...

  5. [转] React 是什么

    用脚本进行DOM操作的代价很昂贵.有个贴切的比喻,把DOM和JavaScript各自想象为一个岛屿,它们之间用收费桥梁连接,js每次访问DOM,都要途径这座桥,并交纳“过桥费”,访问DOM的次数越多, ...

  6. SQL中Union与Union All的区别

    在写SQL查询语句时,经常会碰到类似于这种的需求:查询年龄大于60岁的男职工以及所有出生于1950年的职工.在处理这种需求时,无法使用一条简单的SQL语句查询出所有满足条件的结果,此时就需要将这种需求 ...

  7. hdu 1542

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 题意: 求所给矩形的覆盖面积 题解: 利用扫描线的思想,先将坐标离散化,之后以y轴分成多个矩形求解, ...

  8. 【AtCoder】CODE FESTIVAL 2017 qual A

    A - Snuke's favorite YAKINIKU -- #include <bits/stdc++.h> #define fi first #define se second # ...

  9. python全栈开发day12-函数的有用信息、带参数的装饰器、多个装饰器装饰一个函数、global和nonlocal的进一步解析和总结

    1.上周回顾 1).函数名的应用 直接打印函数名,是函数的地址 变量 函数的参数 函数的返回值 可以当容器类数据类型的元素 2).闭包 内层函数对外层函数的非全局变量的引用,就是闭包. 并返回内部函数 ...

  10. python日期与时间

    1.介绍 Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表 ...