如何抑制使用g++的纯虚拟类的c++ vtable生成?

时间:2022-11-25 11:16:53

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?

可以使用__declspec(novtable)属性在MSVC中生成c++ vtable。然而,对于GNU c++编译器,似乎没有等价的属性。事实上,在__cxa_abort()和其他许多类中,把vtables留给纯粹的虚拟类是不必要的链接,我希望避免这种情况,因为我正在为嵌入式系统编程。那么,我该怎么办呢?

struct ISomeInterface
{
    virtual void Func() = 0;
};

class CSomeClass : public ISomeInterface
{
    virtual void Func();
}

void CSomeClass::Func()
{
    //...
}

2 个解决方案

#1


3  

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

有一些东西将实现类似的结果:#pragma接口。然而,pragma实现可以覆盖这一点。http://www.emerson.emory.edu/services/gcc/html/CPP_Interface.html

#2


0  

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

编译器标志-fno-rtti停止运行时类型信息生成。

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).

在我在嵌入式平台上使用c++的经验中,这阻止了vtable编译器错误的发生,这表明它阻止了它们的创建(因此,虚拟函数无法工作)。

#1


3  

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

有一些东西将实现类似的结果:#pragma接口。然而,pragma实现可以覆盖这一点。http://www.emerson.emory.edu/services/gcc/html/CPP_Interface.html

#2


0  

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

编译器标志-fno-rtti停止运行时类型信息生成。

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).

在我在嵌入式平台上使用c++的经验中,这阻止了vtable编译器错误的发生,这表明它阻止了它们的创建(因此,虚拟函数无法工作)。