Does Visual C++ provide a language construct with the same functionality as `__attribute__((alias))` in GCC?












2














__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.











share|improve this question




















  • 2




    Possible duplicate of Does Windows have a __declspec equivalent to Unix GCC's __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..
    Nov 20 '18 at 2:28










  • @R.. - may be i mistake. however this - stackoverflow.com/a/12397639/6401656 is answer for current question
    – RbMm
    Nov 20 '18 at 7:19










  • @RbMm: "same solution works" doesn't imply question is duplicate.
    – R..
    Nov 20 '18 at 12:30
















2














__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.











share|improve this question




















  • 2




    Possible duplicate of Does Windows have a __declspec equivalent to Unix GCC's __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..
    Nov 20 '18 at 2:28










  • @R.. - may be i mistake. however this - stackoverflow.com/a/12397639/6401656 is answer for current question
    – RbMm
    Nov 20 '18 at 7:19










  • @RbMm: "same solution works" doesn't imply question is duplicate.
    – R..
    Nov 20 '18 at 12:30














2












2








2







__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.











share|improve this question















__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.








c++ c gcc visual-c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:44









Peter Ruderman

10.1k2352




10.1k2352










asked Nov 19 '18 at 19:34









LehksLehks

578




578








  • 2




    Possible duplicate of Does Windows have a __declspec equivalent to Unix GCC's __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..
    Nov 20 '18 at 2:28










  • @R.. - may be i mistake. however this - stackoverflow.com/a/12397639/6401656 is answer for current question
    – RbMm
    Nov 20 '18 at 7:19










  • @RbMm: "same solution works" doesn't imply question is duplicate.
    – R..
    Nov 20 '18 at 12:30














  • 2




    Possible duplicate of Does Windows have a __declspec equivalent to Unix GCC's __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..
    Nov 20 '18 at 2:28










  • @R.. - may be i mistake. however this - stackoverflow.com/a/12397639/6401656 is answer for current question
    – RbMm
    Nov 20 '18 at 7:19










  • @RbMm: "same solution works" doesn't imply question is duplicate.
    – R..
    Nov 20 '18 at 12:30








2




2




Possible duplicate of Does Windows have a __declspec equivalent to Unix GCC's __attribute__((weak))?
– RbMm
Nov 19 '18 at 19:51




Possible duplicate of Does Windows have a __declspec equivalent to Unix GCC's __attribute__((weak))?
– RbMm
Nov 19 '18 at 19:51




1




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




#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..
Nov 20 '18 at 2:28




@RbMm: That doesn't look lkke a duplicate.
– R..
Nov 20 '18 at 2:28












@R.. - may be i mistake. however this - stackoverflow.com/a/12397639/6401656 is answer for current question
– RbMm
Nov 20 '18 at 7:19




@R.. - may be i mistake. however this - stackoverflow.com/a/12397639/6401656 is answer for current question
– RbMm
Nov 20 '18 at 7:19












@RbMm: "same solution works" doesn't imply question is duplicate.
– R..
Nov 20 '18 at 12:30




@RbMm: "same solution works" doesn't imply question is duplicate.
– R..
Nov 20 '18 at 12:30












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381461%2fdoes-visual-c-provide-a-language-construct-with-the-same-functionality-as-a%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.






share|improve this answer























  • 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
















1














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.






share|improve this answer























  • 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














1












1








1






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 7:04

























answered Nov 20 '18 at 7:44









P.WP.W

11.7k3842




11.7k3842












  • 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


















  • 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
















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




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




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




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


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381461%2fdoes-visual-c-provide-a-language-construct-with-the-same-functionality-as-a%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory