消除未使用的虚函数 [英] eliminate unused virtual functions

查看:161
本文介绍了消除未使用的虚函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了消除未使用的(普通的)函数,我可以使用:
-ffunction-sections,-fdata-section和-gc-sections。
和它工作。

To eliminate unused (ordinary) function I can use: -ffunction-sections, -fdata-section and --gc-sections. and it works.

我知道使用多态性,函数是后期绑定,所以我想没有办法决定哪个函数

I know that using polymorphism, function are 'late-binding' so I suppose there is no way to decide which of the function can be remove during linkage process.

但我使用纯虚函数来强制类继承实现一些功能。然后在代码中我使用对象(不是指针/引用对象,所以我不使用多态)。

But I am using pure virtual function to force class which inherits to implement some function. Then in code I am using objects (not pointer/reference to object, so I am not using polymorphism).

伪代码:

class BASE {
    ...
    virtual void do_sth() = 0;
    virtual void do_sth_else() = 0;
    ...
};

class C1 : BASE {
    ...
    void do_sth() { //some code }
    void do_sth_else() { //some code }
}

main()
{
    //the do_sth_else function is never used in main
    C1 obj1;
    obj.do_sth();
}

在链接过程中是否有一些方法来消除这个未使用的函数(do_sth_else)
也许我误解了一些东西。因为我认为应该有一种方法来删除这个未使用的函数。如果是这样,请解释我为什么,当我不使用指针与虚拟函数没有办法摆脱多态性开销。 :)

Is there some method to eliminate this unused functions (do_sth_else) during linkage process? Maybe I misunderstood something. and because of that I think there should be a way to remove this unused function. If so please explain me why, when I am NOT using pointers with virtual function there is no way to "get rid" of polymorphic overhead. :)

FYI:此代码主要用于学习目的。

FYI: This code is mainly for learning purpose.

推荐答案

p>感谢 Jonathan Wakely 我开始挖掘,我找到了gcc选项:

Thanks to Jonathan Wakely I started digging and I found gcc options:


-fvtable-gc
发出vtables和虚函数引用的特殊重定位,以便链接器可以识别未使用的虚拟函数并清空引用它们的vtable插槽。这是最有用的-ffunction-sections和-Wl, - gc-sections,以便也放弃函数本身。

-fvtable-gc Emit special relocations for vtables and virtual function references so that the linker can identify unused virtual functions and zero out vtable slots that refer to them. This is most useful with -ffunction-sections and -Wl,--gc-sections, in order to also discard the functions themselves.

但是在GCCv4.7.1

But it is not supported in GCCv4.7.1

这篇关于消除未使用的虚函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆