0

I see people asking questions similar to this but not quite and always with code more complicated than mine. I have reduced my problem to the simplest possible terms:

Using Linux GCC compiler, this will build fine:

class MyBase
{
    void Clear() {}
};

class MyDerived : public MyBase
{
    void Clear() {}
};

This will not:

class MyBase
{
    virtual void Clear() {}
};

class MyDerived : public MyBase
{
    void Clear() {}
};

The link stage produces the following error:

/tmp/ccmEFi6P.o:(.rodata._ZTI9MyDerived[_ZTI9MyDerived]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/ccmEFi6P.o:(.rodata._ZTI6MyBase[_ZTI6MyBase]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
collect2: error: ld returned 1 exit status

Sorry, I am just getting used to the formatting system here.

OK, the compiler I am using is gcc and when I enter "gcc --version" I get:

gcc (GCC) 4.9.1
Copyright (C) 2014 Free Software Foundation, Inc.

The problem popped up while working with a much larger set of source files but I always like to try and reduce the code to the smallest bit as possible that still reproduces the problem. I have done this with a single source file:

derive.cpp:

class MyBase
{
    virtual void Clear() {}
};

class MyDerived : public MyBase
{
    void Clear() {}
};

int main()
{
    MyDerived md;
    return 0;
}

That is literaly all there is. It will build if I remove the "virtual" specifier from the base class' declaration of "Clear()". It will not build if I put it back. In both cases, my command line is:

gcc - o derive derive.cpp

and the full text of the build output is:

/tmp/cckxCssh.o:(.rodata._ZTI9MyDerived[_ZTI9MyDerived]+0x0): undefined 
reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/cckxCssh.o:(.rodata._ZTI6MyBase[_ZTI6MyBase]+0x0): undefined reference 
to `vtable for __cxxabiv1::__class_type_info'
collect2: error: ld returned 1 exit status

Any help would be appreciated.

Jethro63
  • 21
  • 5
  • Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) for more on asking questions that will attract quality answers. Would you please [edit your question](https://stackoverflow.com/posts/43665957/edit) to include a bit more information? What GCC and libc versions; what `uname`; what compiler command line? Also, please add the rest of the source you are trying to compile and link. Is everything in one file, or are `MyBase` and `MyDerived` in separate files? Thanks! – cxw Apr 27 '17 at 19:07
  • 1
    Related questions: http://stackoverflow.com/search?q=%5Bc%2B%2B%5D+undefined+reference+to+vtable+for+__cxxabiv1%3A%3A__class_type_info – Barmar Apr 27 '17 at 19:07
  • The common theme seems to be that this is related to use of RTTI and/or linking C++ with C code. – Barmar Apr 27 '17 at 19:08
  • Did you literally use "gcc - o derive derive.cpp"? Did you try removing the space between '-' and 'o'? – George Skoptsov Apr 27 '17 at 22:19

1 Answers1

0

You are using gcc to compile C++ code, which is a bad idea (as explained, for example, here). Your code compiles and links fine using

g++ -o derive derive.cpp
Community
  • 1
  • 1
user8153
  • 4,049
  • 1
  • 9
  • 18
  • gcc - o derive derive.cpp – Jethro63 Apr 27 '17 at 19:43
  • Thanks. It looks like that was the probem. – Jethro63 Apr 27 '17 at 20:13
  • gcc will recognize .cpp files as C++ and compile/link them accordingly. The likely problem is the typo in your command line. You have an extra space between '-' and 'o'. – George Skoptsov Apr 27 '17 at 22:23
  • 1
    @George Did you try to compile the provided code or look at the related question I linked? The error does not come from the extra space in the compilation command. gcc will indeed compile C++ code, but it will not choose the correct backend which leads to the error at the linking stage. In particular, it will not link to libstdc++. You can fix this by using `gcc -o derive derive.cpp -lstdc++`, or simply use `g++` instead. – user8153 Apr 27 '17 at 23:16