Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I suppress C++ vtable generation for pure virtual classes using G++?

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()
{
    //...
}
like image 861
fincs Avatar asked Dec 03 '11 22:12

fincs


People also ask

Does every class have a vtable?

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.

What does undefined reference to vtable mean?

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.

What is VPTR and vtable where it stored?

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 .

Does every object have a vtable?

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.


1 Answers

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

like image 135
Dave Avatar answered Sep 19 '22 07:09

Dave