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

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. hdu2476

    /* dp[l][r]表示将任意串的[l,r]刷成s2样子的最小代价 ans[i]表示将s1的前i位刷成s2的代价 按照区间dp的常用做法,dp[l][r]的状态由dp[l][k],dp[k+1][r ...

  2. 检查Rabbitmq中队列及消息个数,还有清空的方法

    rabbitmqctl list_queues rabbitmqctl purge_queue kafka_queue (不推荐)使用reset方式重置rabbitmq即清空掉rabbitmq所有的队 ...

  3. requirejs模块路径配置问题

    三种情况:一.设置data-main,没配置baseUrl,以data-main的文件为基准:二.设置data-main,配置baseUrl,baseUrl以值以引用require.js的HTML为基 ...

  4. [CodeChef]GERALD07/[JZOJ4739]Ztxz16学图论

    题解: 考虑从小到大枚举右端点 对于每个点,令它的权值等于它的编号 那么我们可以用lct维护出一颗最大生成树 维护方法是每次插入一条判断他们在不在一颗树上 若不在直接加,若在就找到链上的最小值 之后看 ...

  5. java:打印菱形图案(传参打印的自定义字符和行数)

    打印菱形图案: 代码实现: public class Hello { public static void main(String args[]) { LingXingPrint("#&qu ...

  6. python全栈开发day23-面向对象高级:反射(getattr、hasattr、setattr、delattr)、__call__、__len__、__str__、__repr__、__hash__、__eq__、isinstance、issubclass

    一.今日内容总结 1.反射 使用字符串数据类型的变量名来操作一个变量的值. #使用反射获取某个命名空间中的值, #需要 #有一个变量指向这个命名空间 #字符串数据类型的名字 #再使用getattr获取 ...

  7. js基础梳理-究竟什么是执行上下文栈(执行栈),执行上下文(可执行代码)?

    日常在群里讨论一些概念性的问题,比如变量提升,作用域和闭包相关问题的时候,经常会听一些大佬们给别人解释的时候说执行上下文,调用上下文巴拉巴拉,总有点似懂非懂,不明觉厉的感觉.今天,就对这两个概念梳理一 ...

  8. C/C++指针参数赋值问题

    今天遇到一个问题,即在C/C++中,关于在函数里对指针赋值的问题.首先可以看到如下现象: void test(int *p) { p = NULL; } int main(int argc, char ...

  9. 4712: 洪水 基于链分治的动态DP

    国际惯例的题面:看起来很神的样子......如果我说这是动态DP的板子题你敢信?基于链分治的动态DP?说人话,就是树链剖分线段树维护DP.既然是DP,那就先得有转移方程.我们令f[i]表示让i子树中的 ...

  10. Python3练习题系列(07)——列表操作原理

    目标: 理解列表方法的真实含义. 操作: list_1.append(element) ==> append(list_1, element) mystuff.append('hello') 这 ...