title: Does LLVM/Clang support the 'weak' attribute for weak linking?
body: <p>In brief: does LLVM/Clang support the 'weak' attribute?</p>
  <p>I'm learning some Arduino library sources (HardwareSerial.cpp to be more detailed) and I've found some interesting attribute <code>weak</code> that I've never used before:</p>
  <pre><code>#if defined(HAVE_HWSERIAL0)
    void serialEvent() __attribute__((weak));
    bool Serial0_available() __attribute__((weak));
  #endif
  </code></pre>
  <p>I've found it interesting and I've read that the linker should set it to NULL if it's not defined.</p>
  <p>However, in my tests with Clang I'm unable to use it.</p>
  <p>File <em>lib.cpp</em>:</p>
  <pre><code>#include &quot;lib.h&quot;
  #include &lt;stdio.h&gt;
  
  void my_weak_func() __attribute__((weak));
  
  void lib_func() {
      printf(&quot;lib_func()\n&quot;);
  
      if (my_weak_func)
          my_weak_func();
  }
  </code></pre>
  <p>File <em>lib.h</em>:</p>
  <pre><code>#ifndef LIB_FUNC
  #define LIB_FUNC
  
  void lib_func();
  
  #endif
  </code></pre>
  <p>File <em>main.cpp</em>:</p>
  <pre><code>#include &quot;lib.h&quot;
  #include &lt;stdio.h&gt;
  
  #ifdef DEFINE_WEAK
  void my_weak_func() {
      printf(&quot;my_weak_func()\n&quot;);
  }
  #endif
  
  int main() {
  
      lib_func();
  
      printf(&quot;finished\n&quot;);
      return 0;
  }
  </code></pre>
  <p>If I use <code>g++ lib.cpp main.cpp -o main -DDEFINE_WEAK</code> I'm able to use it:</p>
  <pre class="lang-none prettyprint-override"><code>MBA-Anton:Weak_issue asmirnov$ ./main
  lib_func()
  my_weak_func()
  finished
  </code></pre>
  <p>But if I use <code>g++ lib.cpp main.cpp -o main</code> I'm unable to link the application:</p>
  <pre class="lang-none prettyprint-override"><code>Undefined symbols for architecture x86_64:
    &quot;my_weak_func()&quot;, referenced from:
        lib_func() in lib-ceb555.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  <p>To be more detailed about Clang:</p>
  <pre class="lang-none prettyprint-override"><code>MBA-Anton:Weak_issue asmirnov$ g++ --version
  Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1
  Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
  Target: x86_64-apple-darwin14.3.0
  Thread model: posix
  </code></pre>
  <p><strong>What should I do? Is the <code>weak</code> attribute supported by LLVM/Clang?</strong></p>
  <p>PS. I've already tried to rewrite <em>lib.cpp</em> in the way <a href="https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html" rel="nofollow noreferrer">Apple describes</a> and still get the same linker error:</p>
  <pre><code>#include &quot;lib.h&quot;
  #include &lt;stdio.h&gt;
  
  extern void my_weak_func() __attribute__((weak_import));
  
  void lib_func() {
      printf(&quot;lib_func()\n&quot;);
  
      if (my_weak_func != NULL)
          my_weak_func();
  }
  </code></pre>