title: Same symbols in different libraries and linking order
body: <p>I have 2 libraries: <code>test.1</code> and <code>test.2</code>. Both libraries contain a single global <em>extern "C"</em> <code>void f();</code> function, with different implementations (just a <code>cout</code> for the test).</p>
  
  <p>I did the following test:</p>
  
  <p><strong>Test 1</strong> Dynamic linking:<br>
  If I add <code>libtest.1.so</code> and then <code>libtest.2.so</code> in the makefile of the executable and then call <code>f();</code> in <code>main</code>, <code>libtest.1.so-&gt;f()</code> is called.<br>
  If I change the order in the makefile, <code>libtest.2.so-&gt;f()</code> is called</p>
  
  <p><strong>Test 2</strong> Static linking:<br>
  Absolutely the same happens with static libraries </p>
  
  <p><strong>Test 3</strong> Dynamic loading<br>
  As the library is manually loaded, everything works as expected.</p>
  
  <hr>
  
  <p>I expected an error for multiple definitions, which obviously didn't happen.  </p>
  
  <p>Also, this does not break the one-definition-rule, as the situation is different.</p>
  
  <p>It's also not a dependency-hell(not that it's related to this at all), nor any linking fiasco..</p>
  
  <p>So, than what is this? Undefined behavior? Unspecified behavior? Or it really does depend on the linking order?</p>
  
  <p>And is there a way to easily detect such situations?</p>
  
  <hr>
  
  <p>Related questions:<br>
  <a href="https://stackoverflow.com/questions/18095151/dlopen-vs-linking-overhead">dlopen vs linking overhead</a><br>
  <a href="https://stackoverflow.com/questions/21214902/what-is-the-difference-between-dynamic-linking-and-dynamic-loading">What is the difference between dynamic linking and dynamic loading</a><br>
  <a href="https://stackoverflow.com/questions/7216973/is-there-a-downside-to-using-bsymbolic-functions">Is there a downside to using -Bsymbolic-functions?</a><br>
  <a href="https://stackoverflow.com/questions/45135/linker-order-gcc">Why does the order in which libraries are linked sometimes cause errors in GCC?</a><br>
  <a href="https://stackoverflow.com/questions/6538501/linking-two-shared-libraries-with-some-of-the-same-symbols">linking two shared libraries with some of the same symbols</a></p>
  
  <hr>
  
  <p><strong>EDIT</strong> I did two more tests, which confirm this UB:</p>
  
  <p>I added a second function <code>void g()</code> in <code>test.1</code> and NOT in <code>test.2</code>.</p>
  
  <p>Using dynamic linking and <code>.so</code> libs, the same happens - <code>f</code> is called with the same manner, <code>g</code> is also executable (as expected).</p>
  
  <p>But using static linking now changes the things: if <code>test.1</code> is <em>before</em> <code>test.2</code>, there are no errors, both functions from <code>test.1</code> are called.<br>
  But when the order is changed, "multiple definitions" error occurs.</p>
  
  <p>It's clear, that "no diagnostic required" (see @MarkB's answer), but it's "strange" that sometimes the error occurs, sometimes - it doesn't.</p>
  
  <p>Anyway, the answer is pretty clear and explains everything above - UB.</p>