4

__attribute__((alias)) means:

alias ("target")

The alias attribute causes the declaration to be emitted as an alias for another symbol, which must be specified. For instance,

  void __f () { /* Do something. */; }
  void f () __attribute__ ((weak, alias ("__f")));

defines f to be a weak alias for __f. In C++, the mangled name for the target must be used. It is an error if __f is not defined in the same translation unit.

Not all target machines support this attribute.

Peter Ruderman
  • 12,241
  • 1
  • 36
  • 58
Lehks
  • 2,582
  • 4
  • 19
  • 50

1 Answers1

2

You can do something like this for C. This is supported for x86 and x64 for msvc v19.15.

#include <stdio.h>

void __f() { puts("This function is aliased"); }

void f();

#pragma comment(linker, "/alternatename:f=__f")

int main()
{
    f();
}

See the compiled demo here.

I have tested this in Visual Studio 2017 with /TC option.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • compile nothing prove here. need link, because `f` if not found redirected to `__f` at link time. also `#pragma comment(linker, "/alternatename:f=__f")` only for *x64* and *c* code. for *c++* code symbols will be mangled `?f@@YAXXZ=?__f@@YAXXZ` must be. for *c* and *x86* also will be mangle depend from calin convention. say `_f=___f` for __cdecl or `_f@0=___f@0` for __stdcall and `@f@0=@__f@0` for __fastcall – RbMm Nov 20 '18 at 13:06
  • I tested it on my PC. But I obviously could not link it here. rextester has an older version of VC++ compiler. – P.W Nov 20 '18 at 13:07
  • your current code will be ok only if you compile as *c* code and target platform *x64* otherwise you got unresolved external symbol – RbMm Nov 20 '18 at 13:10