Search code examples
c++linuxlinkervirtualabi

How to instruct the compiler to generate an alias for a virtual function?


See this question for the background.

Basically, I have the following definition of a class

class  MyClass {
    virtual int foo4(double, int);
};

Is there a way to instruct the compiler to generate two symbols that would resolve to foo4? That is, I want that if an executable asks for the dynamic linker to resolve _ZN7MyClass4foo4Edi (symbol for MyClass::foo4(double, int)) and some other symbol (let's say _ZN7MyClass9reserved1Ev, a symbol for MyClass::reserved1()), the dynamic linker would resolve both to &MyClass::foo4(double, int). I`m using fairly modern GCC on Linux.


Solution

  • In C++ it looks like this:

    class  MyClass {
        int foo5(double, int) __attribute__((alias("_ZN7MyClass4foo4Edi")));
        virtual int foo4(double, int);
    };
    
    int MyClass::foo4(double d, int i)
    {
    }