Skip to main content
gamedev.net
🔒 Locked

Link errors

Started by neoGriff Oct 28, 2000 at 11:48 AM 3 replies 350+ views
Original Post
neoGriff
neoGriff
I''m not a newbit to link errors or MSVC++, but I''m having a problem that is really annoying the crap out of me. I want to have an include file with a couple of global variables, and multiple source files in my project need to use these variables. The problem is that whenever more than one source file includes this global.h file, it gives me link errors stating that the variables were already defined earlier. I tried to fix this by doing something like this: #ifndef GLOBAL_H #define GLOBAL_H //global definitions #endif Unfortunately, this did not help at all. Now I''m thinking that I will need to find a setting in MSVC that fix this problem for me. This has happened to me in every project for years now that I''ve tried to use global variables in. Thanks in advance to anyone that can solve this problem for me. Mike Griffin griff@umr.edu
goir
goir
Make sure that the global varibales are declared first in the normal way. And all other files that use those variables have to declare them too, but with the extern keyword infront of it.

Exampel:

// file1

int global;


// file2 want to use "global" from file1

extern int global;
Profus
Profus
In C++ each variable has to be declared exactly once in each project (no matter how many files). You''ll get errors on any compiler if you declare anything more often than once or not at all.
The solution is the one goir proposed. But in addition to what he said I recommend !never! to place global vars in header files. You can observe the result of doing this w/ the fps2d tile engine for allegro (took me hours to find out that the errors I was getting in my multifile project were due to such a stupid design of the lib I was using and not my own code).
So if you feel you need global vars declare them in your main *.cpp file or any other *cpp file you think they fit in and if you also need them in other files do what goir said...
------------------------------------------------------------"To a computer, chaos is just another kind of order."
OGAPO
OGAPO
Neo,

The compiler definitions (i believe) are local to the current .cpp file, so if you''re using more than one .cpp in your project it will still include the header. However, the compiler treats the global namespaces of all files as one, and as such you can only declare everything once. Basically, what you need to do is *declare* all your variables in one and only one of your cpp files and then copy these declarations to the header file with "extern" in front of them. Then just include the header as usual. the externs will be interpreted multiple times, but that''s ok because they all refer to only one defined variable. Hope that makes sense



Brought to you by: OGAPO

+------------------------------+
| Surgeon General's Warning - |
| OGAPO can cause serious |
| mental damage if taken in |
| large doses. |
+------------------------------+


/* Never underestimate the power of stupid people in large groups */
Brought to you by: [email=ogapo@ithink.net]O.G.A.P.O.[/email] +----------------------------------------+| Surgeon General's Warning - || OGAPO can cause serious mental damage || if taken in large doses. |+----------------------------------------+/* Never underestimate the power of stupid people in large groups */
neoGriff
neoGriff
Thanks everyone. I got it to work, and I finally know what "extern" is used for.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.