weak_alias函数做了什么以及它定义在何处

时间:2016-01-20 06:02:20

标签: c linux gcc compiler-construction gnu

所以我正在查看gcc编译器的来源,我在fork.c中看到了这个:

int
__fork ()
{
  __set_errno (ENOSYS);
  return -1;
}
libc_hidden_def (__fork)
stub_warning (fork)

weak_alias (__fork, fork)
#include <stub-tag.h>

我正在试图找出weak_alias的作用。我在glibc源文件中使用了grep命令来查找所有出现的#define weak_alias:

grep -r "#define weak_alias"

我发现很多次出现的宏:

#define weak_alias(n, a)

但宏并没有真正解释任何事情。他们只是定义了这个陈述,但没有说明它是如何被替换的。例如,一次出现在profil.c中:

/* Turn off the attempt to generate ld aliasing records. */
#undef weak_alias
#define weak_alias(a,b)

所以有什么想法,weak_alias做什么以及定义在哪里?

提前致谢

2 个答案:

答案 0 :(得分:11)

来自https://github.com/lattera/glibc/blob/master/include/libc-symbols.h

let config = Realm.Configuration(
    // Set the new schema version. This must be greater than the previously used
    // version (if you've never set a schema version before, the version is 0).
    schemaVersion: 1,

    // Set the block which will be called automatically when opening a Realm with
    // a schema version lower than the one set above
    migrationBlock: { migration, oldSchemaVersion in
        // We haven’t migrated anything yet, so oldSchemaVersion == 0
        switch oldSchemaVersion {
        case 1:
            break
        default:
            // Nothing to do!
            // Realm will automatically detect new properties and removed properties
            // And will update the schema on disk automatically
            self.zeroToOne(migration)
        }
})

let realm = try! Realm(configuration: config) // Invoke migration block if needed

关于弱势符号:

https://en.wikipedia.org/wiki/Weak_symbol

答案 1 :(得分:1)

它是执行以下操作的宏:

它声明了一个弱函数,如果您没有为该函数提供强符号名,它将调用您已调用它的函数。例如

int _foo(){ return 1;}

//And weak alias
int __attribute__((weak, alias("_foo"))) foo();

因此,如果您尚未提供foo的实际实现,它将基本上使用_foo并返回1。