小编Rob*_*bin的帖子

Mac OS X上的弱符号链接

目前我在使用Xcode 4.0.2的Mac OS X 10.6.7上遇到了弱链接问题.

robin@chameleon:/tmp/o$ gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
Run Code Online (Sandbox Code Playgroud)

正如文档http://developer.apple.com/library/mac/#technotes/tn2064/_index.html所说,我们可以使用gcc 属性((weak_import))作为弱链接符号.但是,以下示例代码始终抛出编译错误.如下:

weak.c:

#include <stdlib.h>
#include <stdio.h>

extern int SayHello() __attribute__((weak));

int main()
{
    int result;

    if (SayHello!=NULL)
    {
        printf("SayHello is present!\n");
        result=SayHello();
    }
    else
        printf("SayHello is not present!\n");
}
Run Code Online (Sandbox Code Playgroud)

错误消息如下:

robin@chameleon:/tmp/o$ gcc weak.c 
Undefined symbols for architecture x86_64:
  "_f", referenced from:
      _main in cceOf2wN.o
     (maybe you meant: __dyld_func_lookup)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned …
Run Code Online (Sandbox Code Playgroud)

macos linker linker-errors weak

10
推荐指数
1
解决办法
4110
查看次数

标签 统计

linker ×1

linker-errors ×1

macos ×1

weak ×1