Thread: Linker Error - Undefined Reference

  1. #1
    Registered User
    Join Date
    Aug 2010

    Linker Error - Undefined Reference

    Yes I know there are threads on this but ive viewed multiple ones and i just don't get it. This is my first time operating with things like this and my teacher didn't even explain it to us so im lost.

    Basically I have a program, but all of my references to the stuff in the given .h files give a linker error.

    For example,

    in externs.h there is a segment of declarations like this:

    extern int            Agent;              ///< Current user
    extern event_type     Event;              ///< Current event
    extern char*          Op_Names[];         ///< Table of operator names
    extern char*          Event_Names[];      ///< Table of event names
    extern char*          Prog_Names[];       ///< Program file pointers
    extern char*          Prog_File_Names[];  ///< Name of program files
    extern int            Num_Devices;        ///< Number of devices 
    extern int            Num_Terminals;      ///< Number of user terminals

    Now I need to use these things in my obj1.c which has
    #include "externs.h"

    (amongst others)
    Interrupt( )
               //Pointer to the head of the list.
               event_list *head;
               head = Event_List;
               //Setting Clock, Agent, and Event to their respective fields
               Agent = head->agent;
               Event = head->event;
               Clock = head->time;
             //other code unimportant to the question im asking.           
    and everything i reference from externs.h gives me a linker error. Is there a step I'm missing? Do I need to do something with the files to make them correctly link? Once again sorry for creating a new thread but I couldn't get it from what i saw online so i had to ask for help on my specific case.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    For extern references to work, you actually have to have the thing you are referencing some place. Read this: External variable - Wikipedia, the free encyclopedia

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2010
    OK thanks. Now I just need to find out if they already are defined somewhere that i don't know or if i have to make them myself.

    but at least now i know, thanks.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    You either need to link in an object file or library that contains the actual variables (the "extern" versions just describe the type so the compiler can make sure you're using them correctly). If you're using gcc, you can do something like:
    $ gcc -Wall obj1.c file_with_definitions_for_extern_variables
    If that file ends in .c, gcc will compile both and link them into one executable. If that file is already an object file (already been compiled), it will only compile obj1.c, then link that with the existing object file and make an executable.

    Note, the -Wall has nothing to do with compiling multiple files. It just turns the warnings all the way up, with is good practice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker Error/Undefined Reference
    By liljp617 in forum C Programming
    Replies: 2
    Last Post: 09-24-2008, 09:02 PM
  2. Problem: [Linker error] undefined reference to ...
    By bigmac(rexdale) in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2008, 12:53 PM
  3. linker error undefined reference to
    By BJtoVisualcC++ in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2007, 11:40 AM
  4. Linker Error (undefined reference)
    By Dae in forum C++ Programming
    Replies: 5
    Last Post: 07-15-2005, 03:50 PM
  5. Dev C++ linker error: undefined reference
    By josh_d in forum Windows Programming
    Replies: 11
    Last Post: 03-11-2004, 11:58 AM