We have a large code base in C++ and after a smallish refactor (one class added and some related methods rewritten), we started getting linker errors on GCC 3 & 4. The linker errors were specifically "missing references to non-virtual thunks" in small sample programs that subclassed classes in our large SDK.
Searching the web didn't give many hints beyond some old GCC bugs that seem to have been resolved.
The attributes of the problem seem to be:
-O2
class Foo: public A, public B {}
toclass Foo: public B, public A {}
The virtual inheritance only appears in a single, very-commonly used base class for reference counting. I have verified that every usage of this class really is virtual public, and not just public inheritance by accident.
Obviously fiddling with the inheritance order is not really solving the problem. What else could it be?
If changing the declaration order of the base classes fix the issue, it probably means that one of the base class doesn't properly define what it declared.
For example, if you have a declaration with a (not virtual) method Func in class A and the same in class B but you never defined it for class A, then the first time the method is called in your child, the class A's version is called, but at link time, the reference is not found. If you change inheritance order, will make the compiler call B::Func instead, which is defined and then the linker will find it.
IMHO, it's a bad design anyway, since there behaviour will be hard to predict and to debug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With