Mail Archives: djgpp/1998/09/21/08:50:29
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 -