delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/21/08:50:29

Message-Id: <199809211254.NAA18252@rochefort.ns.easynet.net>
Comments: Authenticated sender is <mert0407 AT sable DOT ox DOT ac DOT uk>
From: "George Foot" <george DOT foot AT merton DOT ox DOT ac DOT uk>
To: KillerColt <coulter1 AT globalserve DOT net>
Date: Fri, 18 Sep 1998 16:20:48 +0000
MIME-Version: 1.0
Subject: Re: help please
Reply-to: mert0407 AT sable DOT ox DOT ac DOT uk
CC: djgpp AT delorie DOT com

On 17 Sep 98 at 23:02, KillerColt wrote:

> What does UNDEFINED REFERENCE mean and how do I fix it?

It means that you declared and used a symbol in a source file, 
but didn't actually define it anywhere.  When the linker tried 
to link your object files together it found the reference to 
the symbol but couldn't find the definition of the symbol.

For example:

    extern int i;
    
    int main (void)
    {
        i = 1;
        return 0;
    }

In this example, the `extern' line declares that a variable 
`i' should exist somewhere in the program, but not necessarily 
in this source file.  The `main' function then sets this 
variable to 1.  This is legal, provided a definition for `i' 
does exist somewhere else.  If you compile and link this single 
source file you'll get "undefined reference" because the linker 
never found a definition for `i'.  You need to link it with a 
file containing a definition, i.e.:

    int i;

This also happens with functions.  If you misspell them you 
should first notice "implicit declaration" warnings, but if you 
prototype them and use them without defining them you'll get 
"undefined reference".

In particular though, a cause of this error is forgetting to 
link in a library.  For example, if you use Allegro's "blit" 
function but don't link in the Allegro library you'll get the 
error.  Also if you write C++ code using parts of the C++ 
library but don't link it in (either using gxx or g++ or by 
explicitly linking the libraries) you'll get the same error.

If you'd posted the whole text of the error it would have been 
possible to say which of these you are doing.

-- 
george DOT foot AT merton DOT oxford DOT ac DOT uk

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019