title: Normal or anormal behaviour of dynamic libraries with C++ static objects
body: <p>I'm developing a framework heavily based on plugin implemented as dynamic libraries (on Linux, .so files). All was working fine until I made a plugin depending on another plugin and, as the plugins were implemented as a static instance of a dedicated class, I found the following problem that I reduced to a the following small set of code:</p>
  
  <p>Source of liba.so is here:</p>
  
  <pre><code>class A {
  public:
    A(void) {
      cerr &lt;&lt; "A(" &lt;&lt; this &lt;&lt; ")\n";
    }
  };
  
  A x;
  int y;
  </code></pre>
  
  <p>Source of libb.so is below and depends on liba.so:
      extern int y;</p>
  
  <pre><code>class B {
  public:
    B(void) {
      cerr &lt;&lt; "B(" &lt;&lt; this &lt;&lt; ")\n";
      y = 0;
    }
  };
  
  B x;
  int z;
  </code></pre>
  
  <p>Notice that both classes provides the same symbol x with different type. This situation may happen when one library evolves and unexpectedly uses a new symbol that conflicts with an unknown library or code using it.</p>
  
  <p>Writing a small program using libb.so:</p>
  
  <pre><code>extern int z;
  
  int main(void) {
    z = 0;
    cerr &lt;&lt; "main\n";
    return 0;
  }
  </code></pre>
  
  <p>Executing it gives:</p>
  
  <pre><code>casse@ares ~/tmp/tso $ ./main 
  A(0x7f689dfc605c)
  B(0x7f689dfc605c)
  main
  </code></pre>
  
  <p>Both constructors of x in liba.so and libb.so are executed and display the "this" address and they display the same address! Is this normal behavior? It seems strange that the constructors of static objects are confused by two storage with the same name. Is it a bug? GCC or Linux?</p>
  
  <p>Thanks to whoever can help me.</p>