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

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. Innodb引擎下mysql自身配置优化

    1.简单介绍InnoDB给MySQL提供了具有提交,回滚和崩溃恢复能力的事务安全(ACID兼容)存储引擎.InnoDB锁定在行级并且也在SELECT语句提供一个Oracle风格一致的非锁定读.这些特色 ...

  2. Laravel Eloquent 自定义返回字段

    返回指定字段 Book::select("price", "name")->all(); 返回关系字段关联的属性 Book::select("p ...

  3. python 全栈开发,Day108(客户管理之权限控制,客户管理之动态"一级"菜单,其他应用使用rbac组件,django static文件的引入方式)

    一.客户管理之权限控制 昨天的作业,有很多不完善的地方 下载代码,基本实现权限验证 https://github.com/987334176/luffy_permission/archive/v1.2 ...

  4. POJ 3126 Prime Path (素数+BFS)

    题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数. 思路:先打表求出四位长度的所有素数,然后利用BFS求解.从a状态入队,然后从个位往千位的顺序 ...

  5. 【BZOJ4927】第一题 双指针+DP

    题解: 虽然是过了,不过做的十分智障 首先是有 2根 2 1 1 , 3根 1 1 1 这两种方法 然后考虑2 2 1 1 two-point-two没啥好说的 3 1 1 1 我很智障的以为数据范围 ...

  6. python全栈开发day36-IO多路复用

    一.复习 1.进程.线程.协程 进程:是计算机中最小的资源分配单位,数据隔离,可以利用多核,数据不安全 线程:是计算机中最小的CPU调度单位,数据共享,GIL,数据不安全 协程:是线程的一部分,是由用 ...

  7. drupal笔记

    $app_root :网站根目录 安装 汉化:1将汉化包放置drupal8\sites\default\files\translations下安装:2极简版的话需要在extend(扩展)中安装Inte ...

  8. 编辑你的数学公式——markdown中latex的使用

    前言 最近开始使用起markdown来记学习笔记,因为经常有公式要写,就需要用到latex,到网上查来查去又不太方便,而且也很少能查到写的比较全的,就准备写下这篇文章. 插入数学公式 在markdow ...

  9. Linux/Window 正斜杠 反斜杠

    文件目录结构: Linux 是用正斜杠 目录名区分大小写 Window 是用反斜杠 目录名不区分大小写

  10. Stream 与 byte[] 互转

    public byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(byt ...