Sep 30, 2019

[C++] error message: VTT is missing

Reference:
https://quuxplusone.github.io/blog/2019/09/30/what-is-the-vtt/
VTable Notes on Multiple Inheritance in GCC C++ Compiler v4.0.1
Itanium C++ ABI: 2.6.2 VTT Order
[SO] What is the VTT for a class?
[SO] What is a C++ “Key Function” as described by gold?

The memory layout of virtual inheritance and it's mechanism has been mentioned in detail in Stanley's Inside the C++ Object Model Ch 5.2.

Here's the code generates error message (Clang++10.0.1)
Undefined symbols for architecture x86_64:
  "VTT for Derived"
#include <iostream>
using namespace std;

struct VirtualBase{
    VirtualBase(){
        cout << "v-base" << endl;
    }
};

struct Base : virtual VirtualBase {
    // Or code like this:
    //  __attribute__((noinline)) Base() {}
    Base() {
        run();
    }

    virtual void run(){
        cout << "run" << endl;
    }
};

struct Derived: Base{
    Derived(){
        cout << "Derived" << endl;
    }

    virtual void drun();
};

// remove this comment will trigger the error.
// This is due to the virtual table needs the address of the
// Derived::drun() function.
// void Derived::drun(){}


int main() {
    Derived{};
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.