g++ probelm (not wine)

The Ghost In The Machine ewill at lexideb.athghost7038suus.net
Tue Nov 13 09:44:45 CST 2001


Followups redirected to a more appropriate newsgroup.

In comp.emulators.ms-windows.wine, Bence Fejervari
<bentse at sch.bme.hu>
 wrote
on Mon, 12 Nov 2001 10:25:02 +0100 (CET)
<Pine.LNX.4.30.0111121022320.5997-100000 at rip.sch.bme.hu>:
>
>Hi,
>
>I'm sorry for posting this problem to the list, but I'm slowly getting
>mad.
>
>So my problem:
>rip:bentse:~/program/B-Tree $ )`g++': g++ BTree.cpp proba.cpp
>SearchTree.cpp
>/tmp/ccZwWxpf.o: In function `BT_BTree_c::BT_BTree_c(ST_BuildCompare_c
>*)':
>/tmp/ccZwWxpf.o(.text+0x19): undefined reference to `BT_BTree_c virtual
>table'
>/tmp/ccZwWxpf.o: In function `BT_BTree_c::~BT_BTree_c(void)':
>/tmp/ccZwWxpf.o(.text+0x75): undefined reference to `BT_BTree_c virtual
>table'
>/tmp/ccZ9aNru.o: In function `ST_BuildCompare_c::ST_BuildCompare_c(void)':
>/tmp/ccZ9aNru.o(.ST_BuildCompare_c::gnu.linkonce.t.(void)+0x8): undefined
>reference to `ST_BuildCompare_c virtual table'
>collect2: ld returned 1 exit status
>
>All the classes are well defined (I checked them many times).
>What does undefined reference to `BT_BTree_c virtual table' mean?

Most likely, it means that you've forgotten to implement the
first declared virtual function as counted from the vtbl in
in the (possibly ancestral) baseclass, which is different from
the first virtual function declared in your class.

For example:

x.h:

class X { public: virtual ~X(); ... };

y.h:

#include "x.h"

class Y : public X
{ public: ~Y(); virtual void m()=0; virtual void n(); ... };

z.h:

#include "y.h"

class Z : public Y
{ public: void m(); };

x.C:

#include "x.h"

X::~X()
{
}

y.C:

#include "y.h"

// Y::~Y()
// {
// }

void Y::n()
{
}

z.C:

#include "z.h"

void Z::m()
{
}

main.C:

#include "z.h"

int main(int argc, char **argv) { Z z; z.m(); z.n(); return 0; }

will elicit the error (among others):

undefined reference to `Y virtual table'

as the implementation to Y::~Y() is missing.  The GNU compiler
uses the first virtual function in the vtbl that is declared
in the class to "hang the vtbl from", as an optimization (that way,
if a class implementation is spread out over multiple files, as
can happen with complicated implementations, one doesn't get
multiply defined references).

If one comments out Z::m() in a similar fashion, one gets
the error

undefined reference to `Z virtual table'

In the case of this example, Z's vtbl looks a bit like this
(X doesn't have the last two methods):

Vtbl+0: type_info
Vtbl+4: destructor, offset 0
Vtbl+12: method 'void m()', offset 0
Vtbl+20: method 'void n()', offset 0

and X's vtbl is hung off X::~X(),
Y's vtbl is hung off Y::~Y() (or would be), and
Z's vtbl is hung off Z::m().

(The offsets are important for multiple inheritance.)

There may be options to modify this behavior somewhat, but it's
usually not an issue.  I'd have to look for the details myself
('info g++').  I suspect this behavior is not specific to GNU G++.

[.sigsnip]

-- 
ewill at aimnet.com -- insert random misquote here
EAC code #191       1d:06h:14m actually running Linux.
                    Use the source, Luke.



More information about the wine-users mailing list