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

不羁岁月 提交于 2020-01-03 08:58:19

问题


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()
{
    //...
}

回答1:


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




回答2:


The compiler flag -fno-rtti stops run-time type information generation.

In my experience with C++ on embedded platforms, this has prevented vtable compiler errors from occurring, suggesting it prevents them from being created (and consequentially, virtual functions won't work).



来源:https://stackoverflow.com/questions/8371470/how-do-i-suppress-c-vtable-generation-for-pure-virtual-classes-using-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!