1

我正在我的库(GCC 4.4.4-14 ubuntu 5.1)上编译 ac 接口,它基本上看起来像这样

标题

extern "C" 
{
  int foo()
}

执行

int foo()
{}

int bar() __attribute__((alias ("foo")));

这编译得很好,但是当我将共享对象链接到测试应用程序并尝试调用 bar() 时,无法识别该函数。

我尝试将别名行从实现移动到标题,然后出现编译器错误:

bar() aliased to undefined symbol foo

为什么将别名移动到标头会导致此错误?以及如何让这一切正常工作?

4

2 回答 2

2

除了上面 booiljoung 的回答之外,您可能还会发现gcc 网站上的以下信息很有用:

alias ("target") alias 属性导致声明作为另一个符号的别名发出,必须指定。例如, void f () { /* 做某事。*/; } void f() _属性((weak, alias (" _f")));

定义f' to be a weak alias for_f '。在 C++ 中,必须使用目标的错位名称。如果 ` _f' 没有在同一个翻译单元中定义,这是一个错误。

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

于 2012-05-29T16:15:24.747 回答
1

extern "C" 需要执行。

extern "C" 
{
  int foo();
}


extern "C" // <<< extern "C" also!!!
{
  int foo()
  {}
}

int bar() __attribute__((alias ("foo")));
于 2012-05-29T15:43:47.327 回答