undefined reference to virtual table / type_info function

Posted by: Andre81

undefined reference to virtual table / type_info function - 25/08/2005 22:18

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?
Posted by: tonyc

Re: undefined reference to virtual table / type_info function - 25/08/2005 22:24

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..)
Posted by: Andre81

Re: undefined reference to virtual table / type_info function - 25/08/2005 22:53

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
Posted by: tman

Re: undefined reference to virtual table / type_info function - 25/08/2005 23:26

You've not implemented the method and it's complaining.
Posted by: Andre81

Re: undefined reference to virtual table / type_info function - 25/08/2005 23:54

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.
Posted by: peter

Re: undefined reference to virtual table / type_info function - 26/08/2005 07:29

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
Posted by: peter

Re: undefined reference to virtual table / type_info function - 26/08/2005 07:30

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
Posted by: tonyc

Re: undefined reference to virtual table / type_info function - 26/08/2005 11:29

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...
Posted by: Andre81

Re: undefined reference to virtual table / type_info function - 26/08/2005 12:40

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?
Posted by: wfaulk

Re: undefined reference to virtual table / type_info function - 26/08/2005 13:04

Try compiling the C sources with g++, too. It may be a mangling issue.
Posted by: Andre81

Re: undefined reference to virtual table / type_info function - 26/08/2005 13:10

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.
Posted by: wfaulk

Re: undefined reference to virtual table / type_info function - 26/08/2005 13:38

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.
Posted by: Andre81

Re: undefined reference to virtual table / type_info function - 26/08/2005 13:44

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;
}

Posted by: wfaulk

Re: undefined reference to virtual table / type_info function - 26/08/2005 13:47

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.
Posted by: peter

Re: undefined reference to virtual table / type_info function - 26/08/2005 13:51

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
Posted by: Andre81

Re: undefined reference to virtual table / type_info function - 26/08/2005 13:56

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?
Posted by: Andre81

Re: undefined reference to virtual table / type_info function - 26/08/2005 13:58

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".
Posted by: wfaulk

Re: undefined reference to virtual table / type_info function - 26/08/2005 13:59

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.