Globals not being global enough?

Started by
5 comments, last by Obolus 22 years, 9 months ago
Hey folks, I was having a little problem with trying to get my global variables to work properly, and was delighted to find Teej''s clever little global container struct: #if !defined (GLOBALS_OWNERSHIP) extern #endif // !defined(GLOBALS_OWNERSHIP) struct { // ------------------ // Global Variables // ------------------ } Globals; So I put this in a file called Globals.h, which I have now included in all the source files in my project. But, I''m sad to say, this still hasn''t cured my problem. You see, I''m getting those cursed LNK2001 errors as so: Direct3D.obj : error LNK2001: unresolved external symbol "struct __unnamed Globals" (?Globals@@3U__unnamed@@A) Model.obj : error LNK2001: unresolved external symbol "struct __unnamed Globals" (?Globals@@3U__unnamed@@A) Main.obj : error LNK2001: unresolved external symbol "struct __unnamed Globals" (?Globals@@3U__unnamed@@A) Debug/InitDX8.exe : fatal error LNK1120: 1 unresolved externals I *have* defined GLOBALS_OWNERSHIP in my Main.cpp file, *before* the inclusion of Globals.h. Does anyone have ANY idea why this is still going wrong for me???? PLEASE help! Thanks, Gareth
Advertisement

Doncha have to do sumpin like this:

extern Globals g;

????
I don''t think so because the "Globals" thing is actually the instance of the structure itself. It''s an unnamed structure with the intention of having only one instance (or so I''m led to believe). Teej''s original code called this "G" instead of "Globals".

Thanks anyway!
Gareth
You have #define GLOBALS_OWNERSHIP in at least on of the source-files, otherwise every source-file will have the globals-struct marked as extern.

-Neophyte

P.S. You must #define GLOBALS_OWNERSHIP *before* you include the globals.h in that source-file.

- Death awaits you all with nasty, big, pointy teeth. -
Thanks, Neophyte, but I have already done that (it says towards the bottom of my original post). I''m going spare trying to solve this one!

Cheers,
Gareth
Maybe check your spelling in your #defines

The only way that error will generate is if the ''extern'' is not removed from the structure declaration atleast once during compile. Which will only happen if GLOBALS_OWNERSHIP is not defined before you include that file. Unless maybe you are doing some other #ifndef or #ifdef tests in your globals.h and then including the globals.h inside of another include file which is then included before your #define GLOBALS_OWNERSHIP. A good rule is never include a global include file in another include file, always do it in your source files directly.

If you are using MSVC maybe try a full rebuild instead of incremental build. Sometimes fixes wierd linking errors.

Otherwise, post a simple example that you can''t get to work.

Seeya
Krippy
Oops. Sorry for not reading your post properly...

Anyways, try changing the line
#if !defined (GLOBALS_OWNERSHIP)
to
#ifndef GLOBALS_OWNERSHIP

-Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

This topic is closed to new replies.

Advertisement