本文翻译自:When to use extern in C++

I'm reading "Think in C++" and it just introduced the extern declaration. 我正在阅读“在C ++中思考”,它刚刚介绍了extern声明。 For example: 例如:

extern int x;
extern float y;

I think I understand the meaning (declaration without definition), but I wonder when it proves useful. 我想我理解了含义(没有定义的声明),但是我想知道它何时证明有用。

Can someone provide an example? 有人可以提供例子吗?


#1楼

参考:https://stackoom.com/question/hjFK/何时在C-中使用extern


#2楼

It is useful when you share a variable between a few modules. 当您在几个模块之间共享变量时,此功能很有用。 You define it in one module, and use extern in the others. 您可以在一个模块中定义它,而在其他模块中使用extern。

For example: 例如:

in file1.cpp: 在file1.cpp中:

int global_int = 1;

in file2.cpp: 在file2.cpp中:

extern int global_int;
//in some function
cout << "global_int = " << global_int;

#3楼

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; 为了澄清,使用extern int x; tells the compiler that an object of type int called x exists somewhere . 告诉编译器在某处存在一个名为x int类型的对象。 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. 编译完所有源文件后,链接器会将x所有引用解析为它在已编译源文件之一中找到的一个定义。 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. 为了使它起作用, x变量的定义需要具有所谓的“外部链接”,这基本上意味着它需要在函数外部(通常称为“文件范围”)之外声明,并且没有static关键字。

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: 来源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: 来源2:

#include <iostream>
#include "header.h"void print_global_x()
{//print global_x here:std::cout << global_x << std::endl;
}

#4楼

This is useful when you want to have a global variable. 当您想要一个全局变量时,这很有用。 You define the global variables in some source file, and declare them extern in a header file so that any file that includes that header file will then see the same global variable. 您在某个源文件中定义了全局变量,并在头文件中将它们声明为extern,以便任何包含该头文件的文件都将看到相同的全局变量。


#5楼

When you have global variables you have to declare them extern in any translation unit they're not defined in or you'll get multiple definitions. 当您有全局变量时,必须在未定义它们的任何翻译单元中将它们声明为extern,否则您将获得多个定义。 This is of course to be avoided since globals are generally not good. 当然要避免这种情况,因为全局变量通常不好。

When you're writing in C and want to allow C++ users to use your library you declare your stuff in an extern "C" {} block. 当您使用C编写并希望允许C ++用户使用您的库时,您可以在extern "C" {}块中声明您的内容。 Since C sucks you shouldn't need this either. 由于C很烂,您也不需要这个。

Finally, there's declaring a template instantiation that occurs somewhere else and you want to link to it rather than making a new one. 最后,声明了一个模板实例化,该模板实例化发生在其他地方,您想链接到该模板而不是创建一个新的模板。 You declare those extern also. 您也声明那些外部。 This has occasional use...maybe...I never have. 这偶尔会用到...也许...我从来没有用过。

I think I can count the amount of times I've needed "extern" in C++ on one hand since I tend to avoid all constructs in which it's needed. 我想我可以一方面计算在C ++中需要“外部”使用的次数,因为我倾向于避免使用所有需要的构造。


#6楼

It's all about the linkage . 关于链接的一切。

The previous answers provided good explainations about extern . 先前的答案对extern提供了很好的解释。

But I want to add an important point. 但我想补充一点。

You ask about extern in C++ not in C and I don't know why there is no answer mentioning about the case when extern comes with const in C++. 你问externC ++不是在C,我不知道为什么没有答案提这个案子时extern带有const在C ++中。

In C++, a const variable has internal linkage by default (not like C). 在C ++中,默认情况下const变量具有内部链接(与C不同)。

So this scenario will lead to linking error : 因此,这种情况将导致链接错误

Source 1 : 来源1:

const int global = 255; //wrong way to make a definition of global const variable in C++

Source 2 : 来源2:

extern const int global; //declaration

It need to be like this: 它需要像这样:

Source 1 : 来源1:

extern const int global = 255; //a definition of global const variable in C++

Source 2 : 来源2:

extern const int global; //declaration

何时在C ++中使用extern相关推荐

  1. c++中的 extern C

    c++中的 extern "C" 博客分类: c CC++C#GCCD语言  比如说你用C开发了一个DLL库,为了能够让C++语言也能够调用你的DLL输出(Export)的函数,你 ...

  2. C++项目中的extern C {}

    2010-07-10 19:45 by 吴秦, 92864 阅读, 22 评论, 收藏, 编辑 引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: ? 1 2 3 4 5 6 7 8 9 ...

  3. ida 中segment中的extern是什么

    紫色部分: extern不是真正的段它是IDA创建的一个伪段,用于表示其他模块中地址未知的符号: GOT通常包含指向这些符号的指针在调试过程中,它可能会被.bss所覆盖,或者被操作系统加载程序清除堆栈 ...

  4. 嵌入在C++程序中的extern C

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

  5. VLFeat库中加extern “C“

    使用VLFeat的时候官网有这么一句话,VLFeat是C的库,想要在C++中使用它就要加上extern "C" 如下: extern "C" { #includ ...

  6. C中的extern关键字

    main.c中 #include<stdio.h>#include "test.h" int a = 1; int b = 2; int main() {fun();/ ...

  7. Keil C 中全局变量 extern 的使用

    在KEIL C中,有多个源文件使用到全局变量时,可以在一个源文件中定义全局变量,在另外的源文件中用extern 声明该变量,说明该变量定义在别的文件中,将其作用域扩展到此文件. 例如:有以下两个源文件 ...

  8. C与C++中的extern与static、extern C与__cplusplus的作用

    一.概述 以C语言编写的源文件后缀名为.c,以C++语言编写的源文件后缀名为.cpp,C++支持函数的重载,C和C++编译器对函数的编译处理是不完全相同.C++编译后的函数一般是以函数名和形参类型来命 ...

  9. DSP2833x_Device.h头文件中关于extern cregister volatile unsigned int IFR的解释

    看源程序(TMS320F28335)的时候在main函数中看到这样一段: // Disable and clear all CPU interrupts: DINT; IER = 0x0000; IF ...

最新文章

  1. keras pad_sequences
  2. Dockerfile 之 ARG指令详解及示例
  3. 3-12形参和实参 局部变量
  4. MVVM更容易内存泄露吗?
  5. fprintf 和 perror 的理解1
  6. java spring hiberate_Java程序员:Spring Boot和Hibernate一起使用的技巧
  7. mysql和jfinal的区别_mysql与JFinal的数据关系-阿里云开发者社区
  8. [源码和文档分享]基于AVL树表示的集合ADT实现与应用
  9. Visio 与 Access 2007 的集成应用
  10. 使用命令编译运行Java程序
  11. LeetCode(771)——宝石与石头(JavaScript)
  12. map-based exploration of intrinsic shape differences and variability
  13. 争锋职考职称计算机,PVP攻略 | 新秀职业的争锋天下?
  14. Android Training精要(五)讀取Bitmap對象實際的尺寸和類型
  15. 取自开源,分享于开源 —— 利用CVE-2017-8890漏洞ROOT天猫魔屏A1
  16. HTML 图书销售排行榜
  17. 《必然》二、奔跑吧,所有人都是菜鸡
  18. 基于FPGA数字混频器的设计(1)
  19. Facade Design
  20. 计算机网络(第7版) - 第五章 运输层 - 习题

热门文章

  1. 关于Android制作.9.png图片
  2. JUnit4 中@AfterClass @BeforeClass @after @before的区别对比
  3. Android TabLayout(选项卡布局)简单用法实例分析
  4. Android 利用源码调试 详解TouchEvent 事件分发机制
  5. Android防止按钮连续点击
  6. mysql timdir_MySQL备份之mysqlhotcopy与注意事项
  7. mysql 左连接b表的一条数据_如果你正在找MySQL精品资源,那来这里看看
  8. 【Android View事件分发机制】滑动冲突
  9. python 功能 代码_让你的Python代码实现类型提示功能
  10. 2007年3月东北微软技术活动预告