简体   繁体   English

在c ++中使用extern

[英]The usage of extern in c++

I've having difficulty understanding how 'extern' works. 我很难理解'extern'是如何工作的。 I've searched Google but there doesn't seem to be the particular example case I'm trying 我搜索过谷歌,但似乎没有我正在尝试的具体案例

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]; 如果我有一个文件main.cpp引用one.h并且在其中我有一个名为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: main.cpp中:

#include "one.h"

extern double LIST1[100][100];

one.cpp: one.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:错误LNK2001:未解析的外部符号“double(* LIST1)[100]”(?LIST1 @@ 3PAY0GE @ NA)

A variable declaration at namespace scope is always a definition unless you put extern on it; 命名空间范围内的变量声明总是一个定义, 除非你把extern放在它上面; then it's just a declaration. 那它只是一个宣言。

An important rule in C++ is that you can't have multiple definitions of objects with the same name. C ++中的一个重要规则是,您不能对具有相同名称的对象进行多个定义。 If your header file just contained double LIST1[100][100]; 如果你的头文件只包含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 . 但是,只要将头文件包含在多个翻译单元中,就会有多个LIST1定义。 You've broken the rule! 你违反了规则!

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. 这个LIST1声明只是说这个类型的数组存在于某个地方,但实际上我们需要定义它来创建对象。 So in a single translation unit (one of your .cpp files usually), you will need to put: 因此,在单个翻译单元(通常是您的.cpp文件之一)中,您需要放置:

double LIST1[100][100];

Now, each of your .cpp files can include the header file and only ever get the declaration. 现在,每个.cpp文件都可以包含头文件,并且只能获得声明。 It's perfectly fine to have multiple declarations across your program. 在你的程序中有多个声明是完全没问题的。 Only one of your .cpp files will have this definition. 只有一个 .cpp文件具有此定义。

In C++, like C before it, each source file is compiled to an object file. 在C ++中,与之前的C一样,每个源文件都被编译为一个目标文件。 Then all the object files are linked to create the executable program. 然后链接所有目标文件以创建可执行程序。

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. `extern'关键字表示可以访问符号,但不能定义。 It should be defined (as a global) in some other module. 它应该在某些其他模块中定义(作为全局)。 If not, you get an 'undefined symbol' error at link time. 如果没有,您在链接时会收到“未定义的符号”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM