Weak symbol link on Mac OS X-Collection of common programming errors

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

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

As the document http://developer.apple.com/library/mac/#technotes/tn2064/_index.html said, we can use gcc attribute((weak_import)) for weak link symbol. However, the following sample code always throw compile error. As the following:

robin@chameleon:/tmp/o$ cat weak.c 
#include 
#include 

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:

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

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

robin@chameleon:/tmp/o$ gcc -undefined dynamic_lookup weak.c 
robin@chameleon:/tmp/o$ ./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: robin@chameleon:/tmp/o$ 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:

robin@robinz:/tmp/o$ cat weak.c 
#include 
#include 

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");
}
robin@robinz:/tmp/o$ gcc weak.c 
robin@robinz:/tmp/o$ ./a.out 
SayHello is not present!

The symbol of SayHello in binary is:

robin@robinz:/tmp/o$ nm a.out | grep Hello
                 w SayHello

"w" The symbol is a weak symbol that has not been specifically tagged as a weak object symbol.

And I test the old xcode 3.2, it works as expected.

Could anyone help me on this? Was it a bug of ld?

And I found more interested things. When I create a dummy lib to export the SayHello symbol in dynamic lib, it works as expected.

robin@chameleon:/tmp/o$ cat dummy.c 
int SayHello() {
    return;
}
robin@chameleon:/tmp/o$ gcc -dynamiclib -o libdummy.dylib dummy.c 
robin@chameleon:/tmp/o$ gcc weak.c libdummy.dylib 
robin@chameleon:/tmp/o$ ./a.out 
SayHello is present!

If the libdummy.dylib does not exist:

robin@chameleon:/tmp/o$ rm libdummy.dylib 
robin@chameleon:/tmp/o$ ./a.out 
SayHello is not present!

Works as expected! Weak symbol now in nm message, as expected:

robin@chameleon:/tmp/o$ nm -m a.out | grep Hello
                 (undefined) weak external _SayHello (from libdummy)

Originally posted 2013-11-10 00:16:39.