5

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.

Community
  • 1
  • 1
p12
  • 1,161
  • 8
  • 23
  • 1
    Just for the sake of completeness (it's in a comment since you asked about GCC and linux), in VC++ you can do it using pragmas: http://nikoniko-programming.blogspot.com/2010/09/aliasing-symbol-names-during-link-time.html – Asaf Feb 20 '12 at 19:20

2 Answers2

3

In gcc, use the "alias" attribute.

int reserved1() __attribute__((alias ("_ZN7MyClass4foo4Edi")));

... but I believe this will only work in the same object file as (a) definition of the symbol, so I'm not sure it will suit your uses cases: see here. Specifically, it will only be an alias for one version of the virtual call, and won't be inherited by subclasses; additionally, you cannot use it to alias a weak symbol.

Borealid
  • 95,191
  • 9
  • 106
  • 122
1

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)
{
}
Richard Pennington
  • 19,673
  • 4
  • 43
  • 72