14

When compiling in debug mode my xcode compilation has these linking errors:

"<method name>", referenced from: 
Vtable for <classname>in <objectfile.o>

"non-virtual thunk to <method name>", referenced from: 
Vtable for <classname>in <objectfile.o>

the strange thing is: It only occurs in one of my build targets (both targets are pretty much the same for that code), plus if these methods are defined in the header file instead of the .cpp it works fine for both targets.

All of those methods are pure virtual. The class where these errors occur inherits from multiple classes but only one of those causes these errors.

Anyone has any idea of what is causing this error?

Andriy
  • 2,727
  • 2
  • 18
  • 29
rahzark
  • 474
  • 1
  • 4
  • 13

2 Answers2

9

Got hit by the same issue. It simply happened when we defined a virtual member function (in the .h header file) but not implemented it (in the .cpp file).

In my case, the implementation was inside a #define that prevented to be actually compiled. GCC should have more explicit message for that kind of common mistake such as

virtual function <function> defined but not implemented in class <class>
Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
neutrino38
  • 91
  • 1
  • 2
  • 1
    "when we defined (in the .h)" - you meant **declared**. definitions = implementation. – underscore_d Mar 30 '16 at 20:35
  • @underscore_d I think I must diagree. class declaration `class Foo;`. Class definition `class Foo{int a;};` . Class implementation AFAIK is there, where you have method defined, so in .cpp file. Please see http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration. – DawidPi Jan 25 '17 at 10:55
  • @DawidPi But you're talking about something different. The other two of us were talking about the member function, not the class. Specifying the signature of the member function in the _class definition_ is a _function declaration_. – underscore_d Jan 25 '17 at 15:17
  • Ah, yes sure, Sorry then :) – DawidPi Jan 26 '17 at 13:28
0

we'll start with the obvious bits: this suggests that the cpp is not linked in, or that the calls are referenced directly and not defined (you can define a pure virtual).

beyond that, there may be differences in build settings - generally, this is because of default symbol visibility (Xcode alias flags, and recommended settings):

GCC_INLINES_ARE_PRIVATE_EXTERN = NO
GCC_SYMBOLS_PRIVATE_EXTERN = NO

there are a few other build settings which could interfere -- idk how your projects are structured so... this list can become rather large.

justin
  • 101,751
  • 13
  • 172
  • 222
  • Just run into this problem with a linked C++ library. Could you clarify, whether those settings you mention above are the values they should be or the (possibly incorrect) default values? – JOM Sep 18 '13 at 06:49
  • @JOM assume `NO` if you do not know the author's intent. also ensure your exception and rtti info is generated like in the dylib, and finally, verify that the virtual functions of that type all have definitions (if the source is available). – justin Sep 18 '13 at 10:43