Bug 96283 - "undefined vtable" error should indicate which members are missing
Summary: "undefined vtable" error should indicate which members are missing
Status: RESOLVED DUPLICATE of bug 42540
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-07-22 12:45 UTC by Eyal Rozenberg
Modified: 2020-07-22 13:57 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eyal Rozenberg 2020-07-22 12:45:18 UTC
Consider the following code:

class Base {
public:
    virtual void vmethod();
};

class foo : public Base {
    int x;
    void vmethod() override;
};

int main() {
    foo f;
}

This will yield the errors (irrelevant paths snipped):

ld: prog.o: in function `Base::Base()':
<source>:1: undefined reference to `vtable for Base'
ld: prog.o: in function `foo::foo()':
<source>:6: undefined reference to `vtable for foo'

While this is true, it is a bit confusing. But even supposing I looked up what this error means and realized what was going on, I would still need to go over all the methods of one or two of the classes to find the one that's missing its implementation. In this simple example that's not so difficult, but sometimes it's quite the nuisance.

I'm assuming the compiler provides the linker with enough information to realize which virtual methods' implementations are missing, so that the linker can finally print an error message which methods are still missing after it has run.

In this specific case, the linker should complain about vmethod() missing its definition.

GodBolt: https://godbolt.org/z/9Ejn4s
Comment 1 Andrew Pinski 2020-07-22 12:52:22 UTC
(In reply to Eyal Rozenberg from comment #0)
> I'm assuming the compiler provides the linker with enough information to
> realize which virtual methods' implementations are missing, so that the
> linker can finally print an error message which methods are still missing
> after it has run.

It does not.  Also techincally the C++ ABI can really be changed.
Comment 2 Eyal Rozenberg 2020-07-22 13:29:48 UTC
(In reply to Andrew Pinski from comment #1)
> (In reply to Eyal Rozenberg from comment #0)
> > I'm assuming the compiler provides the linker with enough information to
> > realize which virtual methods' implementations are missing
> 
> It does not.  

Ok, still - the linker knows which virtual methods it needs, and it knows which are provided by each compiled translation unit. Isn't that enough?


> Also techinically the C++ ABI can really be changed.

I don't understand how this sentence relates to the previous part of your reply. If you mean - can change between compilation and linking - that's theoretically possible but would be the cause of all sorts of trouble.
Comment 3 Jonathan Wakely 2020-07-22 13:47:55 UTC
(In reply to Eyal Rozenberg from comment #2)
> Ok, still - the linker knows which virtual methods it needs, and it knows
> which are provided by each compiled translation unit. Isn't that enough?

The linker does not need the virtual functions though. It needs the vtable.

Nothing references the virtual functions, so they don't need to be defined.
Comment 4 Jonathan Wakely 2020-07-22 13:51:12 UTC
(In reply to Eyal Rozenberg from comment #0)
> While this is true, it is a bit confusing. But even supposing I looked up
> what this error means and realized what was going on, I would still need to
> go over all the methods of one or two of the classes to find the one that's
> missing its implementation. In this simple example that's not so difficult,
> but sometimes it's quite the nuisance.

But you don't need to go over all of them at all. The error happens because the key function is not defined, so that's the only one that you need to define in order to produce a vtable.

See https://gcc.gnu.org/wiki/VerboseDiagnostics#undefined_reference_to_vtable_for_X

> I'm assuming the compiler provides the linker with enough information to
> realize which virtual methods' implementations are missing, so that the
> linker can finally print an error message which methods are still missing
> after it has run.

The linker doesn't know anything about virtual functions. It doesn't even know anything about a vtable, it just knows there are undefined references to symbols called _ZTV4Base and _ZTV3foo, so it prints those out (after demangling them).

> In this specific case, the linker should complain about vmethod() missing
> its definition.

Th linker is not part of GCC though, so if you want changes to the linker, this is the wrong place to ask for it.
Comment 5 Jonathan Wakely 2020-07-22 13:57:35 UTC
This is a dup.

*** This bug has been marked as a duplicate of bug 42540 ***