Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#263495 - 25/08/2005 22:18 undefined reference to virtual table / type_info function
Andre81
new poster

Registered: 13/08/2005
Posts: 28
Loc: Germany
The C part of my project compiles without a problem but now i've added the cpp files to my project and i get some undefined references for the classes with virtual methods.

For the constructor and destructor i get the linker error
undefined reference to '.... virtual table'

And for the derived classes i get the linker error
undefined reference to '... type_info function' and
undefined reference to '... type_info node'

Any idea?

Top
#263496 - 25/08/2005 22:24 Re: undefined reference to virtual table / type_info function [Re: Andre81]
tonyc
carpal tunnel

Registered: 27/06/1999
Posts: 7058
Loc: Pittsburgh, PA
Quote:
but now i've added the cpp files to my project

There's your problem. Friends don't let friends use C++.

(Sorry for not having anything actually useful to add..)
_________________________
- Tony C
my empeg stuff

Top
#263497 - 25/08/2005 22:53 Re: undefined reference to virtual table / type_info function [Re: tonyc]
Andre81
new poster

Registered: 13/08/2005
Posts: 28
Loc: Germany
I've found a "solution":

-fno-rtti resolved the following errors

undefined reference to '... type_info function' and
undefined reference to '... type_info node'

Then I've removed the constructor and destructor from the base class which resolved the undefined reference to '.... virtual table' error.

But actually while the first might be a solution i would like to have my constructor back

Top
#263498 - 25/08/2005 23:26 Re: undefined reference to virtual table / type_info function [Re: Andre81]
tman
carpal tunnel

Registered: 24/12/2001
Posts: 5528
You've not implemented the method and it's complaining.

Top
#263499 - 25/08/2005 23:54 Re: undefined reference to virtual table / type_info function [Re: tman]
Andre81
new poster

Registered: 13/08/2005
Posts: 28
Loc: Germany
In Antwort auf:
You've not implemented the method and it's complaining.


No, that would be too simple

The type_info function is an internal gcc function and i get a undefined reference to virtual table linker error if i declare a c'tor or d'tor.

Top
#263500 - 26/08/2005 07:29 Re: undefined reference to virtual table / type_info function [Re: Andre81]
peter
carpal tunnel

Registered: 13/07/2000
Posts: 4180
Loc: Cambridge, England
Quote:
The type_info function is an internal gcc function and i get a undefined reference to virtual table linker error if i declare a c'tor or d'tor.

Are you using ld, gcc or g++ as the linker command? You get extra magic if you link using g++. Otherwise I'd suggest that you've failed to implement a method somewhere -- GCC "keys" generation of the vtable to one of the methods (to avoid having to compile it multiple times) and, if it happens to have picked one you haven't implemented, linking will fail.

Peter

Top
#263501 - 26/08/2005 07:30 Re: undefined reference to virtual table / type_info function [Re: tonyc]
peter
carpal tunnel

Registered: 13/07/2000
Posts: 4180
Loc: Cambridge, England
Quote:
There's your problem. Friends don't let friends use C++.

In which case I advise you to get that, almost entirely C++, player binary the heck off your empeg

Peter

Top
#263502 - 26/08/2005 11:29 Re: undefined reference to virtual table / type_info function [Re: peter]
tonyc
carpal tunnel

Registered: 27/06/1999
Posts: 7058
Loc: Pittsburgh, PA
Quote:
In which case I advise you to get that, almost entirely C++, player binary the heck off your empeg

Peter


Hehe. Yes, that'll happen...

Actually, the irony of my post is that I make liberal use of C++ style comments in my code, and when I'm feeling particularly lazy, write C code that only compiles with C++ (mainly so variable declarations can go anywhere.) I just find C++'s particular brand of OOP to be the most vile, disgusting stuff ever. Though I haven't seen Perl6 yet...
_________________________
- Tony C
my empeg stuff

Top
#263503 - 26/08/2005 12:40 Re: undefined reference to virtual table / type_info function [Re: peter]
Andre81
new poster

Registered: 13/08/2005
Posts: 28
Loc: Germany
In Antwort auf:
Are you using ld, gcc or g++ as the linker command? You get extra magic if you link using g++.


I'm using gcc to compile the C sources and g++ to compile the C++ sources and to link.

In Antwort auf:
Otherwise I'd suggest that you've failed to implement a method somewhere -- GCC "keys" generation of the vtable to one of the methods (to avoid having to compile it multiple times) and, if it happens to have picked one you haven't implemented, linking will fail.


It was indeed a missing (but not yet used) method. gcc handles the vtable like cfront, but i thought it always picks the first method, which is in my case always the c'tor?

Top
#263504 - 26/08/2005 13:04 Re: undefined reference to virtual table / type_info function [Re: Andre81]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
Try compiling the C sources with g++, too. It may be a mangling issue.
_________________________
Bitt Faulk

Top
#263505 - 26/08/2005 13:10 Re: undefined reference to virtual table / type_info function [Re: wfaulk]
Andre81
new poster

Registered: 13/08/2005
Posts: 28
Loc: Germany
In Antwort auf:
Try compiling the C sources with g++, too. It may be a mangling issue.


If i do this (compiling C sources with g++) i get several errors:

implicit declaration of function 'int fork(...)'
implicit declaration of function 'int getpid(...)'

I also have to change char * to unsigned char * everywhere in the vfdlib.

Top
#263506 - 26/08/2005 13:38 Re: undefined reference to virtual table / type_info function [Re: Andre81]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
Well, the opposite thing to do is to wrap relevant portions of your C++ code with extern C{}, which won't always work, especially if you're using overloading.

My guess is that you're trying to call C++ functions from C and the C object file is expecting to see myfunc() and the C++ object file is providing gnu_int_int_myfunc() or some such nonsense.

Of course, I could be completely off base.
_________________________
Bitt Faulk

Top
#263507 - 26/08/2005 13:44 Re: undefined reference to virtual table / type_info function [Re: wfaulk]
Andre81
new poster

Registered: 13/08/2005
Posts: 28
Loc: Germany
In Antwort auf:
My guess is that you're trying to call C++ functions from C and the C object file is expecting to see myfunc() and the C++ object file is providing gnu_int_int_myfunc() or some such nonsense.


Try to compile the following code with g++:
Code:

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int main ()
{
if (0 != fork ())
{
return 0;
}
return 0;
}


Top
#263508 - 26/08/2005 13:47 Re: undefined reference to virtual table / type_info function [Re: Andre81]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
I meant that that may be your initial problem. I understand that C++ doesn't support fork() for whatever reason.

But try including unistd.h.


Edited by wfaulk (26/08/2005 13:50)
_________________________
Bitt Faulk

Top
#263509 - 26/08/2005 13:51 Re: undefined reference to virtual table / type_info function [Re: Andre81]
peter
carpal tunnel

Registered: 13/07/2000
Posts: 4180
Loc: Cambridge, England
Quote:
Try to compile the following code with g++:
Code:

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int main ()
{
if (0 != fork ())
{
return 0;
}
return 0;
}



It doesn't compile, because fork() is in <unistd.h>. Once that's corrected, it certainly links. C++ is less forgiving than C of missing declarations, and thus of missing header files.

Peter

Top
#263510 - 26/08/2005 13:56 Re: undefined reference to virtual table / type_info function [Re: wfaulk]
Andre81
new poster

Registered: 13/08/2005
Posts: 28
Loc: Germany
In Antwort auf:
But try including unistd.h.


That resolved the problem. But i wonder why g++ needs the unistd.h header while gcc doesn't need it?

Top
#263511 - 26/08/2005 13:58 Re: undefined reference to virtual table / type_info function [Re: peter]
Andre81
new poster

Registered: 13/08/2005
Posts: 28
Loc: Germany
In Antwort auf:
It doesn't compile, because fork() is in <unistd.h>. Once that's corrected, it certainly links. C++ is less forgiving than C of missing declarations, and thus of missing header files.


Thanks, but i wonder why gcc doesn't generate a warning like "undefined function, assuming extern returning int".

Top
#263512 - 26/08/2005 13:59 Re: undefined reference to virtual table / type_info function [Re: Andre81]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
As Peter said, because gcc is less picky. It assumes that undefined functions return ints, and that's true of fork(). If you give gcc the -Wall flag, it'll complain about that.
_________________________
Bitt Faulk

Top