Singleton Unresolved External in Seperate Library

Oct 15, 2011 at 11:25pm
Hey there everyone, busy implementing a globals class with all the singletons I need in my "Engine" project. I had handled this differently prior, but felt it needed redoing. My issue now is that my "Game" project links to the engine library, however when I attempt to compile it it moans about the static singletons in the Globals.h being unresolved externals.

This is the rough layout of my Globals.h. I'm not quite sure why I'm getting linkage issues with my "Game" project (which doesn't even use any of the singletons at the moment), yet my "Engine" project compiles perfectly. Anyone have any hints as to why this could be happening?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Globals
{
public:

        static void Initialise();
        static void Destroy();

        static Singleton* GetSingleton()
        {
                return singleton;
        }

private:

        static Singleton *singleton;
};
Oct 16, 2011 at 12:02am
You do have to implement those functions somewhere. Have you done so, if so where are they in Game or Engine?

In general, I think Singleton is a much overused pattern and should be avoided where possible.
Oct 16, 2011 at 12:02am
Static members are different in that declaring them is not enough. You need to define them separately in an implementation file, similarly to a global variable with extern linkage.

You need to put something like this in a .cpp file:

Singleton* Globals::singleton = NULL;
Oct 16, 2011 at 12:22am
I was indeed defining the pointers, but for some reason, it's struggling with NULL but 0 seems to get rid of the compiler error?!

Anyone have any thoughts on that? The compiler seems to be spazzing out a bit!

On the topic of singletons, I intend to avoid them in all cases aside from the most major of game and engine components.
Last edited on Oct 16, 2011 at 12:41am
Oct 16, 2011 at 5:26am
Anyone have any thoughts on that?
NULL is a macro that's automatically defined for Visual Studio. Sorry, I should've mentioned that. You can define it yourself #define NULL 0 , or* of course you can simply assign 0 to the pointer. If you're using C++11 then you can use the new nullptr constant.

*Apparently, NULL is defined in <cstring>

http://www.cplusplus.com/reference/clibrary/cstring/NULL/
Last edited on Oct 16, 2011 at 5:38am
Topic archived. No new replies allowed.