public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* GCC 10.2: undefined reference to vtable: missing its key function
@ 2021-06-06 21:41 Paul Smith
  2021-06-07 11:29 ` Paul Smith
  2021-06-07 14:19 ` Paul Koning
  0 siblings, 2 replies; 4+ messages in thread
From: Paul Smith @ 2021-06-06 21:41 UTC (permalink / raw)
  To: gcc

I have a class which is NOT, as far as I can see, polymorphic.

It doesn't inherit from any other class and none of its methods are
declared virtual.  The class implementation and all its callers all
compile just fine.

Is there some other way that a class can be thought to be virtual,
stealthily (say by its usage in some other class or something)?

The problem is that when I link this class I get a linker error from
gold:

  MyClass.h:25: error: undefined reference to 'vtable for MyClass'
ld: the vtable symbol may be undefined because the class is missing its
key function

Line 25 is the first method in the class (a constructor).

Sure enough if I use nm -C on the other object files I'm linking, I see
things like this:

   foo.o:  U vtable for MyClass

How can I figure out why the compiler decides, while compiling foo.cxx,
that MyClass is virtual and needs a vtable when as far as I can tell
it's not (if I use nm on MyClass.o I see no hints of vtable etc.)

This is with GCC 10.2 / binutils 2.35.1 (x86_64)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GCC 10.2: undefined reference to vtable: missing its key function
  2021-06-06 21:41 GCC 10.2: undefined reference to vtable: missing its key function Paul Smith
@ 2021-06-07 11:29 ` Paul Smith
  2021-06-07 14:19 ` Paul Koning
  1 sibling, 0 replies; 4+ messages in thread
From: Paul Smith @ 2021-06-07 11:29 UTC (permalink / raw)
  To: gcc

On Sun, 2021-06-06 at 17:41 -0400, Paul Smith wrote:
> How can I figure out why the compiler decides, while compiling
> foo.cxx, that MyClass is virtual and needs a vtable when as far as I
> can tell it's not (if I use nm on MyClass.o I see no hints of vtable
> etc.)

Err... I think this might have been a problem caused by ccache :(.

Not sure how but cannot reproduce after I deleted my cache.  Sigh.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GCC 10.2: undefined reference to vtable: missing its key function
  2021-06-06 21:41 GCC 10.2: undefined reference to vtable: missing its key function Paul Smith
  2021-06-07 11:29 ` Paul Smith
@ 2021-06-07 14:19 ` Paul Koning
  2021-06-08 11:45   ` Jonathan Wakely
  1 sibling, 1 reply; 4+ messages in thread
From: Paul Koning @ 2021-06-07 14:19 UTC (permalink / raw)
  To: paul; +Cc: gcc



> On Jun 6, 2021, at 5:41 PM, Paul Smith <paul@mad-scientist.net> wrote:
> 
> I have a class which is NOT, as far as I can see, polymorphic.
> 
> It doesn't inherit from any other class and none of its methods are
> declared virtual.  The class implementation and all its callers all
> compile just fine.
> 
> Is there some other way that a class can be thought to be virtual,
> stealthily (say by its usage in some other class or something)?

I may be remembering wrong, but -- doesn't dynamic_cast look for a vtable?  So if you do a dynamic cast mentioning that class you'd get such a reference.  Or is that an error when applied to a non-polymorphic class?

	paul



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GCC 10.2: undefined reference to vtable: missing its key function
  2021-06-07 14:19 ` Paul Koning
@ 2021-06-08 11:45   ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2021-06-08 11:45 UTC (permalink / raw)
  To: paulkoning; +Cc: gcc, Paul Smith

(This should have been on the gcc-help list.)

> I may be remembering wrong, but -- doesn't dynamic_cast look for a vtable?

It uses a vtable if one is present, it can't cause one to be
introduced, so ...

>So if you do a dynamic cast mentioning that class you'd get such a reference.

No.

> Or is that an error when applied to a non-polymorphic class?

You can use dynamic_cast to upcast (i.e. from derived to base) for
non-polymorphic class types. It is an error to downcast (from base to
derived) for a non-polymorphic class type.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-06-08 11:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-06 21:41 GCC 10.2: undefined reference to vtable: missing its key function Paul Smith
2021-06-07 11:29 ` Paul Smith
2021-06-07 14:19 ` Paul Koning
2021-06-08 11:45   ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).