title: Both static variables and global variables show different addresses in dynamic library and static library on Linux?
body: <p>I have encountered on CentOS 6.5. As I have searched online that static variable behaves differently on Windows and on Linux when using dynamic library. That is, Windows would cause duplication of variables and Linux would not, like this one:
  <a href="http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html" rel="nofollow">http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html</a></p>
  
  <p>However, when I wrote a small program to validate this, I found that Linux also causes duplication. Here is my small program, including four files:</p>
  
  <p>(1) A.h</p>
  
  <blockquote>
  <pre><code>#ifndef A_H
  #define A_H
  #include &lt;cstdio&gt; 
  static int b; 
  extern "C" class A { 
      public:
      int mem;
      A() {
          printf("A's address: %p\n", this);
          printf("B's address: %p\n", &amp;b);
      }
      void print() {
          printf("%p: %d\n", this, mem);
      }
      ~A() {
          printf("DELETE A!!!!! %p\n", this);
      }
  }; 
  extern A a;
  #endif
  </code></pre>
  </blockquote>
  
  <p>(2) A.cpp</p>
  
  <blockquote>
  <pre><code>#include "A.h"
  A a;
  </code></pre>
  </blockquote>
  
  <p>(3) d.cpp</p>
  
  <blockquote>
  <pre><code>#include "A.h"
  extern "C" void exec() {
      a.print();
  }
  </code></pre>
  </blockquote>
  
  <p>(4) main.cpp</p>
  
  <blockquote>
  <pre><code>#include "A.h"
  #include &lt;dlfcn.h&gt;
  typedef void (*fptr) ();
  int main() {
      a.mem = 22;
      a.print();
      void *handle;
      handle = dlopen("d.so", RTLD_LAZY);
      fptr exec = reinterpret_cast&lt;fptr&gt;(dlsym(handle, "exec"));
      (*exec)();
      dlclose(handle);
      return 0;
  }
  </code></pre>
  </blockquote>
  
  <p>Here is how I compile and run my program:</p>
  
  <blockquote>
  <pre><code>g++ d.cpp A.cpp -shared -rdynamic -o d.so -ldl -I. -fPIC -g -std=c++1y
  g++ main.cpp A.cpp -ldl -I. -g -std=c++1y
  ./a.out
  </code></pre>
  </blockquote>
  
  <p>Both the dynamic part <code>d.cpp</code> and the static part <code>main.cpp</code> use the variables <code>a</code> and <code>b</code> declared in <code>A.cpp</code> and <code>A.h</code>. And here is the result of the program on my machine:</p>
  
  <blockquote>
  <pre><code>A's address: 0x600f8c
  B's address: 0x600f90
  0x600f8c: 22
  A's address: 0x7fb8fe859e4c
  B's address: 0x7fb8fe859e50
  0x7fb8fe859e4c: 0
  DELETE A!!!!! 0x7fb8fe859e4c
  DELETE A!!!!! 0x600f8c
  </code></pre>
  </blockquote>
  
  <p>This surprises me a lot, because the addresses of global variable <code>a</code> and static variable <code>b</code> should be the same in the dynamic part and the static part. And it seems that modification on <code>a</code> in static part does not effect the <code>a</code> in dynamic part. Would anyone please answer my question, or help find out some mistakes in the program (if any)?</p>
  
  <p>By the way, to be honest, on another project I am working on, I find that addresses of global variables are the same in dynamic library and in static library. But that project is too big and I cannot provide a small program to reproduce the behavior.</p>
  
  <p>Thanks a lot !</p>