c++中extern的用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15841495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The usage of extern in c++
提问by Lilz
I've having difficulty understanding how 'extern' works. I've searched Google but there doesn't seem to be the particular example case I'm trying
我很难理解“extern”是如何工作的。我搜索过谷歌,但似乎没有我正在尝试的特定示例案例
If i have a file main.cpp which references one.h and in it i have a list named LIST1 (which is a double array of 100 x 100) so I have double List1[100][100];
如果我有一个引用 one.h 的文件 main.cpp,其中我有一个名为 LIST1 的列表(它是一个 100 x 100 的双数组),所以我有双 List1[100][100];
how can i use this list in one.cpp please?
请问如何在 one.cpp 中使用此列表?
extern double LIST1[100][100]
is not working :/
不管用 :/
main.cpp:
主.cpp:
#include "one.h"
extern double LIST1[100][100];
one.cpp:
一.cpp:
void one::useList()
{
for(j = 0; j < 100; j++)
{
for(i = 0; i < 100; i++)
{
LIST1[j,i] = 0.5;
}
}
}
This is what I have.
这就是我所拥有的。
Error I'm getting:
我得到的错误:
1>main.obj : error LNK2001: unresolved external symbol "double (* LIST1)[100]" (?LIST1@@3PAY0GE@NA)
1>main.obj : error LNK2001: 未解析的外部符号“double (* LIST1)[100]” (?LIST1@@3PAY0GE@NA)
回答by Joseph Mansfield
A variable declaration at namespace scope is always a definition unlessyou put extern
on it; then it's just a declaration.
命名空间范围内的变量声明总是一个定义,除非你把extern
它放在上面;那么它只是一个声明。
An important rule in C++ is that you can't have multiple definitions of objects with the same name. If your header file just contained double LIST1[100][100];
, this would work as long as you only ever included it in one translation unit. But as soon as you include the header file in multiple translation units, you have multiple definitions of LIST1
. You've broken the rule!
C++ 中的一个重要规则是不能有多个同名对象的定义。如果您的头文件只包含double LIST1[100][100];
,只要您只将它包含在一个翻译单元中,这就会起作用。但是,只要将头文件包含在多个翻译单元中,就会有多个LIST1
. 你违反了规则!
So to have a global variable accessible from multiple translation units, you need to make sure there is only a declaration in the header file. We do this with extern
:
因此,要从多个翻译单元访问全局变量,您需要确保头文件中只有一个声明。我们这样做extern
:
extern double LIST1[100][100];
However, you cannot just include the header and try to use this object because there isn't a definition yet. This LIST1
declaration just says that an array of this type exists somewhere, but we actually need to define it to create the object. So in a single translation unit (one of your .cpp
files usually), you will need to put:
但是,您不能只包含标题并尝试使用此对象,因为还没有定义。这个LIST1
声明只是说某个地方存在这种类型的数组,但我们实际上需要定义它来创建对象。因此,在单个翻译单元(.cpp
通常是您的一个文件)中,您需要输入:
double LIST1[100][100];
Now, each of your .cpp
files can include the header file and only ever get the declaration. It's perfectly fine to have multiple declarations across your program. Only oneof your .cpp
files will have this definition.
现在,您的每个.cpp
文件都可以包含头文件,并且只能获得声明。在你的程序中有多个声明是完全没问题的。只有一个你的.cpp
文件都会有这样的定义。
回答by Javier
In C++, like C before it, each source file is compiled to an object file. Then all the object files are linked to create the executable program.
在 C++ 中,就像之前的 C 一样,每个源文件都被编译为一个目标文件。然后链接所有目标文件以创建可执行程序。
To share symbols (functions, global variables), there are several keywords that tell the compiler which are local to the file, which are private, and which are imported from other file.
为了共享符号(函数、全局变量),有几个关键字告诉编译器哪些是文件本地的,哪些是私有的,哪些是从其他文件导入的。
The `extern' keyword means that a symbol can be accessed, but not defined. It should be defined (as a global) in some other module. If not, you get an 'undefined symbol' error at link time.
`extern' 关键字意味着一个符号可以被访问,但不能被定义。它应该在其他模块中定义(作为全局)。否则,您会在链接时收到“未定义符号”错误。