I'm having a strange situation where using a global object appears to cause unrelated linker errors.
My setup is as follows:
// globals.h
#ifndef GLOBALS_H
#define GLOBALS_H
class Singleton
{
...
};
extern Singleton* g_object;
#endif
// globals.cpp
#include "globals.h"
Singleton* g_object = new Singleton();
// somefile.cpp
#include "globals.h"
void function()
{
g_object->do_some_stuff();
}
It looks like that when I try to use g_object somewhere, I get a lot of "undefined references" from the linker, but they all refer to some other, unrelated parts of the project and have nothing to do with my class or object or the functions that use it.
I feel like I'm missing something obvious and it screws up the objects somehow, but I can't quite figure out what, as far as I can tell declaring a global in an .h
file and then providing the definition in a .cpp
is the approved way of creating globals, what am I doing wrong?
P.S.
I use the following flags:
-c -v -Wall -Wno-strict-aliasing -Weffc++ -Woverloaded-virtual -Wshadow -Wuninitialized -fstack-protector -fshort-wchar -fvisibility=hidden -fms-extensions -xc++ -fno-strict-aliasing -std=c++11 -fpermissive -Wno-narrowing -pthread -g -fPI