-
Linking My Project
OK, so I decided it was about time I learn how to break up my sources into smaller files. I read the article on GameDev and the FAQ here. I have the basic jest of it, but I'm still having trouble with the global variables.
I have a global vector declared in a header file that is included in every source file. When I declare it like this, using extern:
extern vector <unit> myUnits;
I get errors about "undefined references to myUnits"
When I leave off the extern I get errors about multiple definition of myUnits. Not sure what I need to do to fix it.
Thanks,
David
-
You have to have one place where the vector is actually declared. Using extern tells the compiler to just pretend the variable exists because you plan for it to be linked to this module later. So the compiler is all happy to pretend your vector exists and can spit out an intermediate file for you (ie. a .o file), but the linker is what puts everything together.
The linker sees all the extern declarations and has to match them with the actual declaration of the vector, which you should have somewhere. If you have a .cpp file that implements your header file, then you could and/or should declare the variable there.
-
Thanks! That cleared it up for me.