0

To eliminate dead code I use GCC's compiler options -fdata-sections -ffunction-sections and linker option -Wl,--gc-sections. It's work fine except the case of unused methods of virtual classes.

For example,

class A {
public:
    virtual void f();
};

class B: public A {
public:
    void unused();
};

class C {
public:
    void unused();
};

GCC keep B::unused(), but remove C::unused().

How to force GCC remove unused methods of virtual classes?

UPD. I've googled -fvtable-gc option, but it doesn't work.

  • Please post a [SSCCE](http://www.sscce.org/). You did not run the linker on the above code. – Ali Mar 03 '14 at 14:52
  • I am repeating myself: Please post a [SSCCE](http://www.sscce.org/). – Ali Mar 06 '14 at 22:26
  • @Ali: What for? The problem is clear. Anybody can write an example for a several minutes, and check that GCC with `-Os`, `-fdata-sections -ffunction-sections`, and `-Wl,--gc-sections` remove unused free functions and methods of non-virtual classes, but can't remove methods (even non-virtual) from virtual classes (= classes that have at least one virtual method). –  Mar 07 '14 at 12:00
  • -1 *"Anybody can write an example for a several minutes, and check"* If you ask a question, it is *your* job to provide us an example. We are happy to help but please don't make us do your job. – Ali Mar 07 '14 at 12:17
  • @Ali: As I said, the problem is clear, without any code. *Why* you need a compilable source code? *How* it can help? SSCCE isn't always suitable, really. *In my case* a short code piece, that precisely show which functions are eliminated and which are not, is better than a compilable code that will contain a lot of excess and insignificant information. I won't continue the discussion. –  Mar 07 '14 at 14:19
  • I wanted to help. I wanted to run the compiler on the *same* code you are using and look at the generated assembly / binary. Then, I would have tried to trick gcc into doing the right eliminations. This takes time, especially if there is an issue with gcc. **Please don't force me to make up an example as well; that's your job.** Without knowing what you are doing exactly, I can only make guesses. Maybe it is some intricate detail of your code, maybe it is how you invoke the compiler, etc. Fighting with me took you longer than fixing the question and posting an SSCCE. Your choice. – Ali Mar 07 '14 at 14:50

1 Answers1

0

You can't remove virtual methods of a class because you can never know when they might be called through a pointer of a derived class object.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • Both `B::unused()` and `C::unused()` are not virtual. –  Mar 03 '14 at 14:31
  • `B::unused()` and `C::unused()` are virtual. Since they are virtual in the parent class, C++ doesn't require you to re-specify the fact that they are virtual. – Josh Mar 03 '14 at 21:40
  • @Josh: The parent class does not contain `unused()` function. –  Mar 05 '14 at 14:50
  • @bolov *You can't remove virtual methods of a class because you can never know when they might be called through a pointer of a derived class object.* Have you heard of [devirtualization](http://stackoverflow.com/questions/7046739/lto-devirtualization-and-virtual-tables)? I suggest you delete this answer, which anyway should be at best a comment. – Ali Mar 06 '14 at 22:29