Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

c++ - eliminate unused virtual functions

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

pseudo code:

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();
}

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: This code is mainly for learning purpose.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

But it is not supported in GCCv4.7.1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...