What are weak external symbols and when are they generated?
<p>My C++ application uses <code>dlopen</code> and friends to implement a plugin system, where shared libraries are loaded as necessary.The plugins depend on classes that are currently defined in the executable itself. </p>

<p>I was surprised to find, that the plugins successfully compile without somehow "<em>linking the plugin to the executable</em>". In other words, the plugins compile without having a definition for the relevant classes. To investigate the situation, I looked at the symbols of the plugin's object file using <code>nm -m thePlugin.o</code>.</p>

<p>In the output of <code>nm</code>, the symbols that are defined in the executable are listed as <code>(__TEXT,__textcoal_nt) weak external</code>. The wikipedia article on <a href="https://en.wikipedia.org/wiki/Weak_symbol" rel="nofollow" title="Test">weak symbols</a> states </p>

<blockquote>
  <p>When linking a binary executable, a weakly declared symbol does not
  need a definition</p>
</blockquote>

<p>So this seems to be the explanation for why the compilation is successful.</p>

<p><strong>Question A:</strong> In which cases are these weak external symbols generated? Is this a portable mechanism (at least for OS X and Linux)?</p>

<p><strong>Question B:</strong> When my plugin references a statically allocated variable (static class member or global <code>extern</code> variable) that symbol is not <code>weak external</code> but <code>unknown</code> and compilation fails, probably due to the lack of a definition. Can I make these symbols weak external as well or alternatively, can I link my plugins with the binary somehow?</p>