How to solve " a missing vtable usually means the first non-inline virtual member function has no definition."

Viewed 1460

I was trying to see how virtual functions are compiled and it seem I cannot compile a trivial example to a dylib on macOS.

I've been through countless permutations of this code and SO answers but I cannot get this trivial example compile (or rather link) with:

clang -dynamiclib -o test.dylib test.cc

here is the code:

class Animal {
    public:
        virtual void who(){};
};

class Cat : Animal
{
  public:
    virtual void who();
};

void Cat::who() {
    }

and here is the complete error:

clang -dynamiclib -o test.dylib test.cc
Undefined symbols for architecture x86_64:
  "vtable for __cxxabiv1::__class_type_info", referenced from:
      typeinfo for Animal in test-be905f.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for __cxxabiv1::__vmi_class_type_info", referenced from:
      typeinfo for Cat in test-be905f.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
1 Answers

The NOTE is a red herring, I'm afraid. vtable for __cxxabiv1::__class_type_info is a symbol in the C++ standard library, not in your code (std::type_info is a polymorphic class), so this error actually means that you need to link against the C++ standard library.

The simplest way to do this is to change clang to clang++ in the command line you've written. The difference between clang and clang++ is that the former defaults to compiling and linking as C, while the latter defaults to compiling and linking C++, but each are capable of compiling both languages.

Related