Currently I encountered a weak link issue on Mac OS X 10.6.7 with Xcode 4.0.2.

$ gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)

As the developer document said, we can use gcc attribute((weak_import)) for weak link symbol. However, the following sample code always throw compile error.
As the following:

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");
}

The error message is the following:

$ 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 1 exit status

Even if use option -undefined dynamic_lookup, it still throws error at runtime:

$ gcc -undefined dynamic_lookup weak.c 
$ ./a.out 
dyld: Symbol not found: _SayHello
  Referenced from: /private/tmp/o/./a.out
  Expected in: dynamic lookup

Trace/BPT trap

The nm -m message of "a.out" is the following:

$ nm -m a.out  | grep Hello

(undefined) external _SayHello (dynamically looked up)

Which was expected as the following:

(undefined) weak external _SayHello (dynamically looked up)

However, when I compile on Ubuntu with gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5, it works as expected:

weak.c:

                        
                        macos
                        
                        linker
                        
                        linker-errors
                        
                        weak
                        
                    
Edit
Report