Thread: Linking My Project

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    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

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Thanks! That cleared it up for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linking .c's and .h's in a project file
    By dezz101 in forum C Programming
    Replies: 6
    Last Post: 09-12-2008, 08:02 AM
  2. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  3. Replies: 5
    Last Post: 09-14-2006, 09:47 PM
  4. Linking an assembly routine into a GCC project
    By huh in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2002, 03:14 PM