{"id":1319,"date":"2022-08-30T15:15:32","date_gmt":"2022-08-30T15:15:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/weak-symbol-link-on-mac-os-x-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:32","modified_gmt":"2022-08-30T15:15:32","slug":"weak-symbol-link-on-mac-os-x-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/weak-symbol-link-on-mac-os-x-collection-of-common-programming-errors\/","title":{"rendered":"Weak symbol link on Mac OS X-Collection of common programming errors"},"content":{"rendered":"<p>Currently I encountered a weak link issue on Mac OS X 10.6.7 with Xcode 4.0.2.<\/p>\n<pre><code>robin@chameleon:\/tmp\/o$ gcc --version\ni686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)\n<\/code><\/pre>\n<p>As the document http:\/\/developer.apple.com\/library\/mac\/#technotes\/tn2064\/_index.html 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>\n<pre><code>robin@chameleon:\/tmp\/o$ cat weak.c \n#include \n#include \n\nextern int SayHello() __attribute__((weak));\n\nint main()\n{\n    int result;\n\n    if (SayHello!=NULL)\n    {\n        printf(\"SayHello is present!\\n\");\n        result=SayHello();\n    }\n    else\n        printf(\"SayHello is not present!\\n\");\n}\n<\/code><\/pre>\n<p>The error message is the following:<\/p>\n<pre><code>robin@chameleon:\/tmp\/o$ gcc weak.c \nUndefined symbols for architecture x86_64:\n  \"_f\", referenced from:\n      _main in cceOf2wN.o\n     (maybe you meant: __dyld_func_lookup)\nld: symbol(s) not found for architecture x86_64\ncollect2: ld returned 1 exit status\n<\/code><\/pre>\n<p>Even if use option -undefined dynamic_lookup, it still throw error at runtime:<\/p>\n<pre><code>robin@chameleon:\/tmp\/o$ gcc -undefined dynamic_lookup weak.c \nrobin@chameleon:\/tmp\/o$ .\/a.out \ndyld: Symbol not found: _SayHello\n  Referenced from: \/private\/tmp\/o\/.\/a.out\n  Expected in: dynamic lookup\n\nTrace\/BPT trap\n<\/code><\/pre>\n<p>The nm -m message of a.out is the following: robin@chameleon:\/tmp\/o$ nm -m a.out | grep Hello<\/p>\n<pre><code>         (undefined) external _SayHello (dynamically looked up)\n<\/code><\/pre>\n<p>Which was expected as the following:<\/p>\n<pre><code>         (undefined) weak external _SayHello (dynamically looked up)\n<\/code><\/pre>\n<p>However, when I compile on Ubuntu with gcc (Ubuntu\/Linaro 4.4.4-14ubuntu5) 4.4.5, it works as expected:<\/p>\n<pre><code>robin@robinz:\/tmp\/o$ cat weak.c \n#include \n#include \n\nextern int SayHello() __attribute__((weak));\n\nint main()\n{\n    int result;\n\n    if (SayHello!=NULL)\n    {\n        printf(\"SayHello is present!\\n\");\n        result=SayHello();\n    }\n    else\n        printf(\"SayHello is not present!\\n\");\n}\nrobin@robinz:\/tmp\/o$ gcc weak.c \nrobin@robinz:\/tmp\/o$ .\/a.out \nSayHello is not present!\n<\/code><\/pre>\n<p>The symbol of SayHello in binary is:<\/p>\n<pre><code>robin@robinz:\/tmp\/o$ nm a.out | grep Hello\n                 w SayHello\n\n\"w\" The symbol is a weak symbol that has not been specifically tagged as a weak object symbol.\n<\/code><\/pre>\n<p>And I test the old xcode 3.2, it works as expected.<\/p>\n<p>Could anyone help me on this? Was it a bug of ld?<\/p>\n<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>\n<pre><code>robin@chameleon:\/tmp\/o$ cat dummy.c \nint SayHello() {\n    return;\n}\nrobin@chameleon:\/tmp\/o$ gcc -dynamiclib -o libdummy.dylib dummy.c \nrobin@chameleon:\/tmp\/o$ gcc weak.c libdummy.dylib \nrobin@chameleon:\/tmp\/o$ .\/a.out \nSayHello is present!\n<\/code><\/pre>\n<p>If the libdummy.dylib does not exist:<\/p>\n<pre><code>robin@chameleon:\/tmp\/o$ rm libdummy.dylib \nrobin@chameleon:\/tmp\/o$ .\/a.out \nSayHello is not present!\n<\/code><\/pre>\n<p>Works as expected! Weak symbol now in nm message, as expected:<\/p>\n<pre><code>robin@chameleon:\/tmp\/o$ nm -m a.out | grep Hello\n                 (undefined) weak external _SayHello (from libdummy)\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:16:39. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Currently I encountered a weak link issue on Mac OS X 10.6.7 with Xcode 4.0.2. robin@chameleon:\/tmp\/o$ gcc &#8211;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1319","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1319"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1319\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}