Thread: [Dev-C++] Newbie linking errors: 'undefined reference'
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Devin C. <dcc...@ya...> - 2002-02-09 19:44:17
|
Generally, I like to lurk and listen a bit before I post, but this time I'm really at wit's end. My project, a simple game based on the old Lost Worlds booklets, keeps generating undefined reference errors in the linking stage. They look like this: d:/devin/projects/lostworlds/womanqsprototype.o(.data$_vt$11WomanQSBook+0x5c ):womanqsprototy: undefined reference to `BaseBook::Lookup(unsigned short, unsigned short) const' The compiler had no problems with just such a function, but the linker does. (I'm using Dev-C++ 4 with MinGW 2.95.2, with the -v -H flags for the linker.) I don't know what 'undefined reference' means, and nowhere on the Net (that Google can find) is there a document that tells me, either. Sometimes I can temporarily make them go away with cosmetic changes to the code: moving a function definition from the .cpp to the .h, moving a function declaration in the .h, etc., seems to work for individual errors. But whenever I add a new module they come back again, twice as plentiful. I thought it might be STL and vectors, but changing my container implementation to use arrays didn't help. So it's not, as I thought, STL's fault. It might be a link order problem, so I used the Make Makefile function and tried to do things from the command line. However, I can't get make to work with the Dev-C++'s makefiles: it gives me errors like '**18: Multiple targets.' And Dev-C++'s Project Options only asks for -additional- arguments to pass to the linker. So I'm really getting demoralized. This problem has held me up for three days now. I'd very much appreciate some assistance. It has been suggested I provide a code fragment, but since the errors seem to be independent of legality of the code, and only crop up when the projects gets big, I don't see how it would help. However, I will provide such a thing if asked, though I really have no idea what could be relevant to the problem. --Devin Carless _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com |
|
From: David M. <ci...@ya...> - 2002-02-10 10:46:36
|
Undefined reference means that the linker cannot find the function you are talking about a compiler does not have to worry about stuff like that the only time I get a error like that is when I forget to link with whatever library I am using(for now that is Allegro most of the time) make sure the function is visible from wherever you are calling it you may have to use an extern keyword (sorry but I don't know how to use it, my projects have not gotten that big) I have not gotten as far as the STL but I am told not to use Dev-C++'s makefiles >Message: 6 >From: "Devin Carless" <dcc...@ya...> >To: <dev...@li...> >Date: Sat, 9 Feb 2002 14:47:28 -0500 >Subject: [Dev-C++] Newbie linking errors: 'undefined reference' > > > Generally, I like to lurk and listen a bit before I post, but this >time I'm really at wit's end. > > My project, a simple game based on the old Lost Worlds >booklets, keeps generating undefined reference errors in the >linking stage. They look like this: > >d:/devin/projects/lostworlds/womanqsprototype.o(.data$_vt$11WomanQSBook+0x5c >):womanqsprototy: undefined reference to `BaseBook::Lookup(unsigned short, >unsigned short) const' > > The compiler had no problems with just such a function, but >the linker does. (I'm using Dev-C++ 4 with MinGW 2.95.2, >with the -v -H flags for the linker.) I don't know what >'undefined reference' means, and nowhere on the Net (that Google >can find) is there a document that tells me, either. > Sometimes I can temporarily make them go away with cosmetic >changes to the code: moving a function definition from the .cpp to >the .h, moving a function declaration in the .h, etc., seems to work >for individual errors. But whenever I add a new module they >come back again, twice as plentiful. > > I thought it might be STL and vectors, but changing my >container implementation to use arrays didn't help. So it's not, as >I thought, STL's fault. It might be a link order problem, so I >used the Make Makefile function and tried to do things from the >command line. However, I can't get make to work with the >Dev-C++'s makefiles: it gives me errors like '**18: Multiple >targets.' And Dev-C++'s Project Options only asks for >-additional- arguments to pass to the linker. > So I'm really getting demoralized. This problem has held me >up for three days now. I'd very much appreciate some assistance. > > It has been suggested I provide a code fragment, but since the >errors seem to be independent of legality of the code, and only >crop up when the projects gets big, I don't see how it would help. >However, I will provide such a thing if asked, though I really have >no idea what could be relevant to the problem. > >--Devin Carless |
|
From: Ioannis V. <no...@ya...> - 2002-02-09 20:54:23
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Devin Carless > Sent: Saturday, February 09, 2002 9:47 PM > To: dev...@li... > Subject: [Dev-C++] Newbie linking errors: 'undefined reference' > > > > Generally, I like to lurk and listen a bit before I post, > but this time I'm really at wit's end. > > My project, a simple game based on the old Lost Worlds > booklets, keeps generating undefined reference errors in the > linking stage. They look like this: > > d:/devin/projects/lostworlds/womanqsprototype.o(.data$_vt$11Wo > manQSBook+0x5c > ):womanqsprototy: undefined reference to > `BaseBook::Lookup(unsigned short, unsigned short) const' > > The compiler had no problems with just such a function, but > the linker does. (I'm using Dev-C++ 4 with MinGW 2.95.2, > with the -v -H flags for the linker.) I don't know what > 'undefined reference' means, and nowhere on the Net (that Generally "undefined reference" means there is no definition for such thing. In your case BaseBook::Lookup(unsigned short, unsigned short) const Probably it has to do with the graphics library you use. > Google can find) is there a document that tells me, either. > Sometimes I can temporarily make them go away with cosmetic > changes to the code: moving a function definition from the > .cpp to the .h, moving a function declaration in the .h, > etc., seems to work for individual errors. But whenever I > add a new module they come back again, twice as plentiful. > > I thought it might be STL and vectors, but changing my > container implementation to use arrays didn't help. So it's > not, as I thought, STL's fault. It might be a link order No relation to STL. > problem, so I used the Make Makefile function and tried to do > things from the command line. However, I can't get make to > work with the > Dev-C++'s makefiles: it gives me errors like '**18: Multiple > targets.' And Dev-C++'s Project Options only asks for > -additional- arguments to pass to the linker. > So I'm really getting demoralized. This problem has held > me up for three days now. I'd very much appreciate some assistance. > > It has been suggested I provide a code fragment, but since > the errors seem to be independent of legality of the code, > and only crop up when the projects gets big, I don't see how > it would help. However, I will provide such a thing if asked, > though I really have no idea what could be relevant to the problem. > > --Devin Carless Either there is no such thing in the library you use or if the construct is yours you have not provided a definition. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.d2g.com * Alternative URL: http://run.to/noicys |
|
From: Devin C. <dcc...@ya...> - 2002-02-09 21:51:23
|
> BaseBook::Lookup(unsigned short, unsigned short) const
>
> Probably it has to do with the graphics library you use.
>
> Either there is no such thing in the library you use or if the construct
> is yours you have not provided a definition.
That's roughly what I thought. The function is, indeed, mine; I'm not
using a graphics library. (The game is that simple.) I do have a
definition in BaseBook.cpp. The definition refers to an object
contained in the private section of BaseBook, MoveParch, which has
its own Lookup function (I do it this way because subclasses of
BaseBook may want to redefine BaseBook::Lookup...
WomanQSPrototype.cpp is one of these subclasses.)
So plainly the linker isn't /reaching/ the definition. The method I
use does require a few daisy-chained functions (BaseBook::Lookup
calling MoveParch::Lookup, and there are other, three-step ones.)
I don't know if this could make a difference. It does not seem to
depend upon the legality of the code.
Ah. Genius lightbulb. Here's an example of something similar
from a few days back:
bool ManeuverList::SetManeuver( short int ManI, char* NameA, short int
BaseModA)
// Inlining this gives linker error 'Undefined reference
// to ManeuverList::Setmaneuver'. Gotcha!
// Same with the Index typedefl.
{
// Maneuver::Set checks to see if we can
// set this maneuver.
bool b;
b = ManVec[ManI].Set( NameA, BaseModA );
return b;
}
Index was typedefed as 'unsigned short int.' It worked perfectly
well elsewhere. That, and making this function inline caused the
undefined reference linker errors. Previously, I was having trouble
with size(): a while loop explicitly told me both A and B were false
(where B included a size() function,) and A && B was false, /but
while(A && B) would still run through./
I don't know if that's related. But it does show the level of
black-voodoo nbugs I'm running into. Two functions stopped giving
errors when the definitions were moved from the .cpp to the .h. An
undefined reference to the deconstructor went away when I moved
the deconstructor up a few lines! So whatever is wrong, it's subtle -
too subtle for me.
--Devin
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
|
|
From: Ioannis V. <no...@ya...> - 2002-02-09 22:10:00
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Devin Carless
> Sent: Saturday, February 09, 2002 11:55 PM
> To: Ioannis Vranos
> Cc: dev...@li...
> Subject: Re: [Dev-C++] Newbie linking errors: 'undefined reference'
>
> That's roughly what I thought. The function is, indeed,
> mine; I'm not using a graphics library. (The game is that
> simple.) I do have a definition in BaseBook.cpp. The
> definition refers to an object contained in the private
> section of BaseBook, MoveParch, which has its own Lookup
> function (I do it this way because subclasses of BaseBook may
> want to redefine BaseBook::Lookup... WomanQSPrototype.cpp is
> one of these subclasses.)
> So plainly the linker isn't /reaching/ the definition. The
> method I use does require a few daisy-chained functions
> (BaseBook::Lookup calling MoveParch::Lookup, and there are
> other, three-step ones.) I don't know if this could make a
> difference. It does not seem to depend upon the legality of the code.
>
> Ah. Genius lightbulb. Here's an example of something
> similar from a few days back:
>
> bool ManeuverList::SetManeuver( short int ManI, char* NameA, short int
> BaseModA)
> // Inlining this gives linker error 'Undefined reference
> // to ManeuverList::Setmaneuver'. Gotcha!
> // Same with the Index typedefl.
> {
> // Maneuver::Set checks to see if we can
> // set this maneuver.
> bool b;
> b = ManVec[ManI].Set( NameA, BaseModA );
> return b;
> }
>
> Index was typedefed as 'unsigned short int.' It worked
> perfectly well elsewhere. That, and making this function
> inline caused the undefined reference linker errors.
It may be for 1000 reasons, e.g. wrong or absense of definition of
ManVec, wrong operator[] definition for ManVec e.t.c..
> Previously, I was having trouble with size(): a while loop
> explicitly told me both A and B were false (where B included
> a size() function,) and A && B was false, /but while(A && B)
> would still run through./
> I don't know if that's related. But it does show the level
> of black-voodoo nbugs I'm running into. Two functions
> stopped giving errors when the definitions were moved from
> the .cpp to the .h.
Had you included the .cpp file in the project? Also if you have the
definition of a class in a .cpp file you must include a declaration of
the class in the scope you use it. For example:
in the a.cpp file:
class A
{
// definition
};
in the file you use it:
class A;
A a;
//..
An undefined reference to the
> deconstructor went away when I moved the deconstructor up a
> few lines! So whatever is wrong, it's subtle - too subtle for me.
Yes i am sure it is noy error from the compiler... May i suggest a great
C++ book? "The C++ Programming Language" 3rd Edition or Special Edition
by Bjarne Stroustrup, the creator of C++. And he doesn't teaches only
C++ but very useful techniques and software engineering. Definitely a
great book!
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.d2g.com
* Alternative URL: http://run.to/noicys
|
|
From: Devin C. <dcc...@ya...> - 2002-02-09 23:22:37
|
> It may be for 1000 reasons, e.g. wrong or absense of definition of > ManVec, wrong operator[] definition for ManVec e.t.c.. > > Had you included the .cpp file in the project? Also if you have the > definition of a class in a .cpp file you must include a declaration of > the class in the scope you use it. For example: I managed to solve the problem. Three problems, actually, and all within an hour - this has been holding me up several days. Two were not subtle; one was. The first I noticed was that I'd fogotten to give an important destructor its due and had simply declared it as ~BaseBook();. I'd forgotten Destructors Need Definitions Too. Whoops one. The second was when I investigated a called function, rather than the calling function. I thought the calling functions were the problem, since they were all in the same place. But it turns out one function really was undefined. Whoops two. The third was more confusing. Apparently 'inline' isn't as benign as I've been told. Removing the 'inline' keyword from functions removed the corresponding undefined reference errors. But it's working now, and I'm happy as a programmer in a room with no lights sources other than a CRT. > Yes i am sure it is noy error from the compiler... May i suggest a great > C++ book? "The C++ Programming Language" 3rd Edition or Special Edition > by Bjarne Stroustrup, the creator of C++. And he doesn't teaches only > C++ but very useful techniques and software engineering. Definitely a > great book! I have a problem in that area. The university I live near has a wonderful math faculty/CS department. The library isn't good, but there are still 200+ titles dealing with C++. But...they're all out. The C++ section in the library is half a shelf. Cobol has two whole shelves. /Every/ primer or introduction to C++ is out, with a few second-rate exceptions (e.g., compiler-specific books, ones that are years out of date, etc.) I think I'll recall them all. Right before exams. --Devin _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com |