title: Weakly link static library via -weak_library
body: <h2>Question:</h2>
  
  <p>Is it possible to weakly link a static library (Obj-C)?</p>
  
  <h2>Short Details</h2>
  
  <p>I do want my custom static framework (<strong>MyFramework.framework</strong>) to weakly link my other custom static library (<strong>libMyLibrary.a</strong>).</p>
  
  <p>The functionality behind <strong>libMyLibrary.a</strong> is optional and can be omitted if there is <em>NO</em> <strong>libMyLibrary.a</strong> being linked by any 3rd party application that uses <strong>MyFramework.framework</strong>.</p>
  
  <p><a href="https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html" rel="noreferrer">I am using <code>-weak_library</code></a>. My test application complains that static linker is unable to find <strong>MyLibrary</strong>'s symbol <code>MyClass</code> within <strong>MyFramework</strong>'s <code>ABCTracker.o</code> symbol:</p>
  
  <pre><code>Undefined symbols for architecture arm64:
    "_OBJC_CLASS_$_MyClass", referenced from:
        objc-class-ref in MyFramework(ABCTracker.o)
  ld: symbol(s) not found for architecture arm64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>How to correctly setup weak linking?</p>
  
  <h2>Full Details</h2>
  
  <h3>Setup</h3>
  
  <ul>
  <li>The Xcode project builds a static Mach-O binary and bundles it into a static framework. The result is <strong>MyFramework.framework</strong> bundle.</li>
  <li>Other project builds a static Mach-O binary and the result is a static lib file <strong>libMyLibrary.a</strong> with a header <strong>MyLib.h</strong></li>
  <li><strong>libMyLibrary.a</strong> is removed from <strong>MyFramework.framework</strong> target's <em>Build Phases</em> > <em>Link Binary With Libraries</em> (<a href="https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html" rel="noreferrer">as suggested here</a>). Only <strong>MyLib.h</strong> is available to use library's API from the framework's classes</li>
  <li>NO Bitcode is used neither in the framework, nor in the library</li>
  <li><strong>MyFramework.framework</strong>, <strong>libMyLibrary.a</strong> and custom application are all written in Objective-C</li>
  <li>The <strong>MyLib.h</strong> defines just one Objective-C class <code>MyClass</code></li>
  <li><strong>MyFramework.framework</strong> uses <code>MyClass</code> from its own class <code>ABCTracker</code> conditionally checking for symbol availability during runtime, e.g. <code>NSClassFromString(@"MyClass") == NULL</code></li>
  <li><p>From <strong>MyFramework</strong> target's <em>Build Settings</em> I have set <code>Other Librarian Flags</code> and <code>Other Linker Flags</code> to same value <code>-weak_library MyLibrary</code>:</p>
  
  <pre><code>OTHER_LDFLAGS = (
      "-weak_library",
      MyLibrary,
  );
  OTHER_LIBTOOLFLAGS = "-weak_library MyLibrary";
  </code></pre></li>
  </ul>
  
  <h3>Result</h3>
  
  <ul>
  <li><strong>MyFramework.framework</strong> builds OK</li>
  <li><p>After the build I have checked the symbols in the resulting binary and the output was emty (no symbols from the static library were built into static framework binary):</p>
  
  <pre><code>$ otool -L MyFramework.framework/MyFramework | grep MyClass
  </code></pre></li>
  <li><p>Despite that, my test application which is not linked with <strong>MyLibrary</strong> whatsoever, builds with <code>ld</code> error:</p>
  
  <pre><code>Undefined symbols for architecture arm64:
    "_OBJC_CLASS_$_MyClass", referenced from:
        objc-class-ref in MyFramework(ABCTracker.o)
  ld: symbol(s) not found for architecture arm64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre></li>
  </ul>
  
  <p>What am I doing wrong here?</p>
  
  <h3>Other Observations</h3>
  
  <p>In <strong>MyFramework</strong> target I set <code>Other Librarian Flags</code> and <code>Other Linker Flags</code> of  to same value:</p>
  
  <ul>
  <li><code>-lMyLibrary</code>. Result: <code>otool</code> shows the library's symbols are built into the framework (expected).</li>
  <li><code>-weak-lMyLibrary</code>. Result is the same as for <code>lMyLibrary</code> (is it expected?)</li>
  </ul>
  
  <p>In <strong>my application</strong> target I set <code>Other Linker Flags</code> to <code>-force_load MyLibrary</code>. Result: the linker error slightly changes:</p>
  
  <pre><code>ld: file not found: MyClass
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>