external variables?

Started by
7 comments, last by goldsword 21 years, 7 months ago
Hi, when I declare globals I get the LNK2005 error in Visual C++ 6.0, why? For example, the following code will give me this error. // a.h #ifndef __header_a #define __header_a #include "someheader1.h" #include "someheader2.h" struct MyStruct { int x,y,z; }; extern MyStruct g_Struct; #endif // b.h #ifndef __header_b #define __header_b #include "a.h" // get the g_Struct #include "someheader1.h" #include "someheader2.h" g_Struct.x = 25; #endif // main.cpp #include "a.h" #include "b.h" int main() { g_Struct.y = 25; return 0; } DEBUG_WINDOW: Error: LNK2005 4@£43g_Struct$£@£$£@$@ - Already defined in main.obj etc etc What am I doing wrong?
#ifndef (Goldsword_was_here)#define (Goldsword_was_here)cout << "Goldsword was here" << endl;#endif
Advertisement
Harrd to get a grip on that, but you shouldn''t declare g_Struct external in main, as it is also there it is "defined", right? If you have other .cpp files that are going to use g_Struct, you need to declare it external to them, and only them. Someone needs to "own" it. Altough, I''m a bit puzzled on that error, which seems to indicate the opposite.
I don''t declare g_Struct in main, I declare it in a.h, but I would like to use it in main and even other cpps, how do you declare it external to a specific file, can you actully do that? Dont I declare it external in the file I declare it in and it should be available to all other files?? Im really confused
#ifndef (Goldsword_was_here)#define (Goldsword_was_here)cout << "Goldsword was here" << endl;#endif
Add the following to the top of the source file containing main:
MyStruct g_Struct; 

extern indicates a declaration of an entity; the linker requires that there also be a corresponding definition of the object.
well in his code they seems like the same thing.
excuse my ignorance but i''ve never worked with externs.

could you explain the concept further Oluseyi? thanks.

Beginner in Game Development?  Read here. And read here.

 

No, you declare and define it in main.cpp as you include a.h when main.cpp is compiled.

This is an example:

// File: main.cpp
int g_iGlobalInt;

int something(); // defined in something.cpp

void main() {
g_iGlobalInt = 5;
int a = something();
}

// File: something.cpp
extern int g_iGlobalInt;

int something() {
return g_iGlobalInt;
}

The header files are never compiled, they are included into your source files and the compiler don''t know the difference. In the example above, main.cpp "owns" g_iGlobalInt. something.cpp tells the compiler that another (object) file in the project "owns" it, and the linker should do its job to link them together.

As I figure you don''t need the extern now, so remove it, and add a link like: extern MyStruct g_Struct; in all other source (.cpp) files that uses it, except the one that "owns" it (presumably main.cpp).

You should check Teej''s "Hands on interactive tutorials"-forum(or something), he has some really neat ways of dealing with globals and explain well (way better than me) how the compiler/linker and related works. I think you''ll find it in the "Selected C topics" thread.
Hmm, I get another error now, redefinition error (2086), tell me... Say you have a class, we can call it CClass, I type the prototype in CClass.h, and the implamentation in the CClass.cpp and finally I declare a extern instance of the class in CClass.h and would like to use that class in main.cpp how should I setup this? Now, I would do the following...

// cclass.h
#ifndef CCLASS_H
#define CCLASS_H
class CClass
{
void foo();
}
extern CClass g_cclass;
#endif

// cclass.cpp
#include "cclass.h"
void CClass::foo()
{
}

// main.cpp
#include "cclass.h"

int main()
{
g_cclass.foo();
return 0;
}

// ERROR: LNK2005
#ifndef (Goldsword_was_here)#define (Goldsword_was_here)cout << "Goldsword was here" << endl;#endif
CWizard: Thanks, that solved my problem and I know more about what the compiler actully do! Thanks.
#ifndef (Goldsword_was_here)#define (Goldsword_was_here)cout << "Goldsword was here" << endl;#endif
For your class question, I don''t see why you want to declare it extern in the header file. You have your declaration of your class in CClass.h and the definition of it in CClass.cpp. When compiling CClass.cpp you don''t want any instantiation of the class, so you shouldn''t declare any such in the header. When you use the class in, say, main.cpp you simply do CClass MyClass;. Now MyClass "belongs" to main.cpp. If you want to use the object/instance in another source file you then need to tell the compiler what MyClass is, so you put the declaration of it preceeded by extern so the compiler knows it is somewhere else, and not to be defined in the current file.

You seem to be a bit too eager to use extern; first try without then with it, and you''ll learn when it is needed and when it''s not.

This topic is closed to new replies.

Advertisement