本文翻译自: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++. 你问extern
在C ++不是在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