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
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];
how can i use this list in one.cpp please?
extern double LIST1[100][100]
is not working :/
main.cpp:
#include "one.h"
extern double LIST1[100][100];
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)
extern "C" specifies that the function is defined elsewhere and uses the C-language calling convention. The extern "C" modifier may also be applied to multiple function declarations in a block. In a template declaration, extern specifies that the template has already been instantiated elsewhere.
Extern is a keyword in C programming language which is used to declare a global variable that is a variable without any memory assigned to it. It is used to declare variables and functions in header files. Extern can be used access variables across C files.
It's necessary when a C++ function must be called by C code rather than C++ code. Basically, when you want your C++ library to be backwards compatible.
A variable declaration at namespace scope is always a definition unless you put extern
on it; 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. 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!
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 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:
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 one of your .cpp
files will have this definition.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With