title: c++ double delete of files scope variable, linking issue?
body: <p>I am on RHEL 6 with gcc version 4.1.2.</p>
  
  <p>I am facing exact same problem as described in this post. Only difference is I am facing this problem for file scope variable. (The variable declared outside the class but in the file, so that it case be accessible across two to three functions. And this variable is getting double deleted even if I have declared on stack)</p>
  
  <p><a href="https://stackoverflow.com/questions/6714046/c-linux-double-destruction-of-static-variable-linking-symbols-overlap">c++ linux double destruction of static variable. linking symbols overlap</a></p>
  
  <p>In my application, I have similar situation.</p>
  
  <pre><code>static library "slib"
  -- inside this library, there is static object "sobj"
  dynamic library "dlib"
  -- links staticly "slib"
  executable "exe":
  -- links "slib" staticly
  -- links "dlib" dynamicly
  </code></pre>
  
  <p>I suspect, I am also facing same problem. To verify, I need to come up with g++ command which will build the files in above manner. I have tried executing following commands but it does not reproduce the said behavior. </p>
  
  <pre><code>g++ -Wall -c static_lib.cpp
  ar -cvq libtests.a static_lib.o
  
  g++ -Wall -fPIC -c dynamic_lib.cpp 
  g++ -shared -Wl,-soname,libtestd.so.1 -o libtestd.so.1.0 dynamic_lib.o -L. libtests.a
  ln -s libtestd.so.1.0 libtestd.so
  
  g++ -Wall -I. -L. main_exe.cpp -ltestd -ltests -o main_prog
  </code></pre>
  
  <p>I am not sure If I have linked the files correctly or in required mannger. But It is not producing the defect which I want to observe. For example, my version of executable outputs following data.</p>
  
  <pre><code>user:~/problem$ ./main_prog
  CTest() this=134520880
  use
  use
  ~CTest() this=134520880, is Alive
  </code></pre>
  
  <p>Where as make file generated using cmake, outputs following data.</p>
  
  <pre><code>user:~/problem$ ./main_exe 
  CTest() this=134520880
  CTest() this=134520880
  use
  use
  ~CTest() this=134520880, is Alive
  ~CTest() this=134520880, is Dead
  </code></pre>
  
  <p>Could you please help to achieve the same effect. I have tried running makefile -n to print commands but it does not print all commands and stops after building dynamic library. </p>
  
  <p>Any inputs will be apprciated.
  Update:
  I used 'make VERBOSE=1' and observed followed commands to get executed. I don't know why -rdynamic is used but We use the same in our application. Please find commands getting executed below.</p>
  
  <pre><code>/usr/bin/c++    -fPIC -c static_lib.cpp
  /usr/bin/ar cr libstatic_lib.a static_lib.o
  /usr/bin/ranlib libstatic_lib.a
  /usr/bin/c++ -fPIC -c dynamic_lib.cpp
  /usr/bin/c++  -fPIC   -shared -Wl,-soname,libdynamic_lib.so -o libdynamic_lib.so dynamic_lib.o libstatic_lib.a
  /usr/bin/c++    -fPIC -c main_exe.cpp
  /usr/bin/c++ main_exe.o -o main_exe -rdynamic libstatic_lib.a libdynamic_lib.so -Wl,-rpath,/home/amey/c_examples/cmake
  </code></pre>
  
  <p>Additionally, nm tests revealed that symbol has different values.</p>
  
  <pre><code>desktop:~/c_examples/cmake$ nm libdynamic_lib.so |grep _ZN5CTest4testE
  0000000000201048 B _ZN5CTest4testE
  desktop:~/c_examples/cmake$ nm main_exe |grep _ZN5CTest4testE
  0000000000602048 B _ZN5CTest4testE
  desktop:~/c_examples/cmake$ nm libstatic_lib.a |grep _ZN5CTest4testE
  0000000000000000 B _ZN5CTest4testE
  </code></pre>
  
  <p>So two major questions are hauling in my head?
  1) Why there are two constructors? Even if there are two constructors why they are pointing to same object
  2) Is there any compilation sequence problem?</p>