Bug 28973 - Improve error message for missing vtable
Summary: Improve error message for missing vtable
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.35
: P2 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2022-03-16 20:51 UTC by Eyal Rozenberg
Modified: 2022-06-22 06:29 UTC (History)
0 users

See Also:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Eyal Rozenberg 2022-03-16 20:51:59 UTC
Related GCC bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42540

If we write the following program:
struct A {
    virtual void foo() { }

struct B : A {
    void foo() override;

int main() {
    B b;
it compiles, but fails to link:
<source>:10: undefined reference to `vtable for B'
(see https://godbolt.org/z/Mzx3c7354)

Now, the reason the vtable is missing is that the C++ ABI (for x86_64?) dictates that the vtable be defined in the object file which defines the "key function" - the first non-pure, non-inline virtual member function of the class. In this case, it's `B::foo()`.

The error message GNU ld emits is not useful enough. It should let the user know _why_ the vtable is undefined - as the user her/himself is not supposed to have provided a vtable; it's something else that's the problem with what they wrote. So the message should be something like:

"undefined reference to the virtual method table for class `B`. This is due to no object having defined the first virtual method in class `B` which isn't inline nor pure-virtual".

or if somehow the linker can figure it out:

"undefined reference to the virtual method table for class `B`, due to `B::foo()` being undefined"

(Obviously, the suggested wording is just a suggestion, the point is to impart the relevant information.)