【问题标题】:Does Visual C++ provide a language construct with the same functionality as `__attribute__((alias))` in GCC?Visual C++ 是否提供与 GCC 中的 `__attribute__((alias))` 功能相同的语言结构?
【发布时间】:2018-11-19 19:34:58
【问题描述】:

__attribute__((alias)) means:

别名(“目标”)

alias 属性导致声明作为另一个符号的别名发出,必须指定。例如,

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

f 定义为__f 的弱别名。在 C++ 中,必须使用目标的错位名称。如果__f 没有在同一个翻译单元中定义,则会出错。

并非所有目标机器都支持此属性。

【问题讨论】:

标签: c++ c gcc visual-c++


【解决方案1】:

您可以对 C 执行类似操作。x86 和 x64 支持 msvc v19.15。

#include <stdio.h>

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

void f();

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

int main()
{
    f();
}

查看编译后的演示here

我已经在 Visual Studio 2017 中使用 /TC 选项对此进行了测试。

【讨论】:

  • 编译这里没有任何证据。需要链接,因为 f 如果在链接时找不到重定向到 __f#pragma comment(linker, "/alternatename:f=__f") 也仅适用于 x64c 代码。对于 c++ 代码符号将被破坏?f@@YAXXZ=?__f@@YAXXZ 必须是。对于 cx86 也将依赖于 calin 约定。说 _f=___f 代表 __cdecl 或 _f@0=___f@0 代表 __stdcall 和 @f@0=@__f@0 代表 __fastcall
  • 我在我的电脑上测试了它。但我显然无法在这里链接它。 rextester 有一个旧版本的 VC++ 编译器。
  • 只有当你编译为 c 代码和目标平台 x64 时你当前的代码才会正常,否则你得到未解析的外部符号
猜你喜欢
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 2010-09-19
相关资源
最近更新 更多