Supressing C++ vtable generation can be done in MSVC using the __declspec(novtable)
attribute. However, it seems that there is no equivalent attribute for the GNU C++ compiler. The fact is that leaving the vtables for pure virtual classes unnecessarily links in __cxa_abort()
and many others, and I want to avoid this happening because I'm programming for an embedded system. So, what should I do?
struct ISomeInterface
{
virtual void Func() = 0;
};
class CSomeClass : public ISomeInterface
{
virtual void Func();
}
void CSomeClass::Func()
{
//...
}
For every class that contains virtual functions, the compiler constructs a virtual table, a.k.a vtable. The vtable contains an entry for each virtual function accessible by the class and stores a pointer to its definition. Only the most specific function definition callable by the class is stored in the vtable.
In summary, there are three key causes of the "undefined reference to vtable" error: A member function is missing its definition. An object file is not being linked. All virtual functions have inline definitions.
Vptr and Vtable get stored in Data Segment... Vtable is like an array of function pointer. Vtable and Vptr is creating at compile time which will get memory in run time and vtable entries are virtual function addresses .
All classes with a virtual method will have a single vtable that is shared by all objects of the class. Each object instance will have a pointer to that vtable (that's how the vtable is found), typically called a vptr. The compiler implicitly generates code to initialize the vptr in the constructor.
There is something that will achieve a similar result: #pragma interface
.#pragma implementation
can override this, however.
http://www.emerson.emory.edu/services/gcc/html/CPP_Interface.html
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