16

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?

3 Answers 3

12

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>
4
  • 1
    "when we defined (in the .h)" - you meant declared. definitions = implementation. Commented Mar 30, 2016 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 stackoverflow.com/questions/1410563/….
    – DawidPi
    Commented Jan 25, 2017 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. Commented Jan 25, 2017 at 15:17
  • Ah, yes sure, Sorry then :)
    – DawidPi
    Commented Jan 26, 2017 at 13:28
1

Encountered this issue when developing with C++/QtCreator, it was caused by including the same file multiple times in the .pro file. Make sure to check each necessary header/source file is only listed under HEADERS += \ and SOURCES += \ once!

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.

2
  • 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
    Commented Sep 18, 2013 at 6: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
    Commented Sep 18, 2013 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.