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.
Asked
Active
Viewed 989 times
4

Peter Ruderman
- 12,241
- 1
- 36
- 58

Lehks
- 2,582
- 4
- 19
- 50
-
2Possible duplicate of [Does Windows have a \_\_declspec equivalent to Unix GCC's \_\_attribute\_\_((weak))?](https://stackoverflow.com/questions/12396333/does-windows-have-a-declspec-equivalent-to-unix-gccs-attribute-weak) – RbMm Nov 19 '18 at 19:51
-
1`#pragma comment(linker, "/alternatename:f=__f")` must be (where in place f and __f must be exactly mangled names) – RbMm Nov 19 '18 at 19:53
-
@RbMm: That doesn't look lkke a duplicate. – R.. GitHub STOP HELPING ICE Nov 20 '18 at 02:28
-
@R.. - may be i mistake. however this - https://stackoverflow.com/a/12397639/6401656 is answer for current question – RbMm Nov 20 '18 at 07:19
-
1@RbMm: "same solution works" doesn't imply *question* is duplicate. – R.. GitHub STOP HELPING ICE Nov 20 '18 at 12:30
1 Answers
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