Note that there are some explanatory texts on larger screens.

plurals
  1. POWeak symbol link on Mac OS X
    primarykey
    data
    text
    <p>Currently I encountered a weak link issue on Mac OS X 10.6.7 with Xcode 4.0.2.</p> <pre><code>robin@chameleon:/tmp/o$ gcc --version i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3) </code></pre> <p>As the document <a href="http://developer.apple.com/library/mac/#technotes/tn2064/_index.html" rel="noreferrer">http://developer.apple.com/library/mac/#technotes/tn2064/_index.html</a> said, we can use gcc <strong>attribute</strong>((weak_import)) for weak link symbol. However, the following sample code always throw compile error. As the following:</p> <p><strong>weak.c</strong>:</p> <pre class="lang-c prettyprint-override"><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; 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"); } </code></pre> <p>The error message is the following:</p> <pre><code>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 </code></pre> <p>Even if use option <code>-undefined dynamic_lookup</code>, it still throws error at runtime:</p> <pre><code>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 </code></pre> <p>The <code>nm -m</code> message of "a.out" is the following:</p> <pre><code>robin@chameleon:/tmp/o$ nm -m a.out | grep Hello (undefined) external _SayHello (dynamically looked up) </code></pre> <p>Which was expected as the following:</p> <pre><code>(undefined) weak external _SayHello (dynamically looked up) </code></pre> <p>However, when I compile on Ubuntu with gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5, it works as expected:</p> <p><strong>weak.c</strong>:</p> <pre class="lang-c prettyprint-override"><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; 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"); } </code></pre> <p>&nbsp; robin@robinz:/tmp/o$ gcc weak.c robin@robinz:/tmp/o$ ./a.out SayHello is not present!</p> <p>The symbol of SayHello in binary is:</p> <pre><code>robin@robinz:/tmp/o$ nm a.out | grep Hello w SayHello </code></pre> <blockquote> <p>"w" The symbol is a weak symbol that has not been specifically tagged as a weak object symbol.</p> </blockquote> <p>And I test the old xcode 3.2, it works as expected.</p> <p>Could anyone help me on this? Was it a bug of ld?</p> <p>And I found more interested things. When I create a dummy lib to export the SayHello symbol in dynamic lib, it works as expected.</p> <p><strong>dummy.c</strong>:</p> <pre class="lang-c prettyprint-override"><code>int SayHello() { return; } </code></pre> <p>&nbsp;</p> <pre><code>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! </code></pre> <p>If the "libdummy.dylib" does not exist:</p> <pre><code>robin@chameleon:/tmp/o$ rm libdummy.dylib robin@chameleon:/tmp/o$ ./a.out SayHello is not present! </code></pre> <p>Works as expected! Weak symbol now in nm message, as expected:</p> <pre><code>robin@chameleon:/tmp/o$ nm -m a.out | grep Hello (undefined) weak external _SayHello (from libdummy) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload