title: static variables don't work across .so files
body: <pre><code>Class UserClass{
      public:
       int rollNo;
  }
  </code></pre>
  <p>In my application I need only a single object of <strong>UserClass</strong>. It is a 3rd party non-singleton class which I cannot modify.</p>
  <pre><code>File-name: userinclude.h
  -------------------------
  class Base {
  public:
      static UserClass* getUserInstance(){
          static UserClass mUser;
          return &amp;mUser;
      }
  };
  </code></pre>
  <p>1)I have a file <strong>lib.cpp</strong> which uses <strong>userinclude.h</strong>. Inside this file there is a function <strong>print()</strong> which prints the <strong>rollNo</strong>.
  <br>eg. <em><strong>cout&lt;&lt; Base::getUserInstance()-&gt;rollNo;</strong></em></p>
  <p><strong>From lib.cpp, lib.so is created.</strong></p>
  <p>2)I have another file <strong>main.cpp</strong> which also uses <strong>userinclude.h</strong>. Inside this there is a function <strong>setUserRollNo(int r)</strong> which sets the <strong>rollNo</strong>.                                                   <br>eg. <em><strong>Base::getUserInstance()-&gt;rollNo = 5;</strong></em></p>
  <p>From <strong>main.cpp</strong> file <strong>lib.so</strong> is <strong>loaded dynamically</strong> using class-loader.
  From main.cpp, <strong>main.exe</strong> is created.</p>
  <p><strong>Problem:-</strong>
  After running the application, I observed that even if I change the rollNo using setUserRollNo(), the print() always prints 0; After debugging I noticed that getUserInstance() returns different addresses in main.cpp and lib.cpp. Multiple call from same file gives same address but they don’t match between different files. So 2 instances got created.</p>
  <p><strong>I tried to make the mUser as a static member of the class but faced same problem.</strong></p>
  <p>My application is a single process application and so data-segment(where static is stored) should be same for the entire application . Hence the local-static mUser should be created only once and getUserInstance() should return same address always through out the application. But I am surprised seeing this behavior.</p>
  <p><strong>c++ : 14<br>
  os : linux</strong></p>
  <p>Please help me in identifying the issue.</p>