title: Constructor of static variable inside shared library
body: <p>I am curious about whether <strong>static variables</strong> instantiated inside <strong>shared libraries</strong> are instantiated at all.
  First of all, here is the code:</p>
  
  <p><strong>AClass.h</strong></p>
  
  <pre><code>class A {
    public:
      A(int x) { cout &lt;&lt; "A constructor called." &lt;&lt; endl; }
  };
  </code></pre>
  
  <p><strong>MyLib.cpp</strong></p>
  
  <pre><code>#include "AClass.h"
  
  static A StaticVariableA(50);
  </code></pre>
  
  <p>So, with this code plus a <strong>Main.cpp</strong> file (with nothing but a empty main function), I wanted to get the constructor to be called by the initialization of the variable inside <strong>MyLib.cpp</strong>.</p>
  
  <p>I compiled <strong>MyLib.cpp</strong> to a shared library, and linked the main program to it in many ways without success. Given that the two files are already compiled to an object file:</p>
  
  <pre><code>(first compiled MyLib.cpp to a shared library)
  $ g++ -O0 -fPIC -shared -Wl,-soname,libMyLib.so -o libMyLib.so MyLib.o
  
  (Tried to link the two files)
  $ g++ -O0 Main.o -rdynamic libMyLib.so -Wl,-rpath,./
  $ g++ -O0 Main.o -L . -Wl,-rpath,./ -lMyLib
  </code></pre>
  
  <p>No success until now, I ask you: is this possible? If it is not possible, why? How do static variables are handled in a shared library? (I am mainly interested in Linux machines)</p>
  
  <p>(I know that I am not supposed to use shared libraries like this. But I just want to know what it does that this doesn't work)</p>
  
  <p>Strangely enough, <a href="https://github.com/YuKill/gmlos" rel="nofollow noreferrer">this code</a> works. When linking the library <strong>GMLOSStFeatEx</strong>, which is a shared library compiled with <code>/src/FeatureExtractor/</code> files, to the <code>/tools/Dummy.cpp</code>. It calls the constructor of static variables of type <code>RegisterStaticFeature</code>.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>It seems like @Pupsik said, it is being optimized out. However, the code I linked above does not get optimized out. It also doesn't have any functions that are called or referenced by outside code.</p>
  
  <p>In <a href="https://stackoverflow.com/questions/11631161/force-to-link-against-unused-shared-library">this</a> question it suggests the use of the linker flag <code>-no-as-needed</code>, which works. But as it is possible to see below, there is no such flag when compiling <a href="https://github.com/YuKill/gmlos" rel="nofollow noreferrer">the code</a> I mentioned.</p>
  
  <pre><code>/usr/bin/c++   -O0 -g -I/usr/local/include  -fPIC -fvisibility-inlines-hidden 
  -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual 
  -Wno-missing-field-initializers -pedantic -Wno-long-long 
  -Wno-maybe-uninitialized -Wnon-virtual-dtor -Wno-comment -std=c++11 -g
  -fno-exceptions -fno-rtti  -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS 
  -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -L/usr/local/lib     
  CMakeFiles/Dummy.dir/DummyMain.cpp.o  -o Dummy -rdynamic ../src
  /FeatureExtractor/libGMLOSStFeatEx.so ../src/FeatureExtractor
  /libGMLOSDynFeatEx.so ../src/Support/libGMLOSCfgPrinter.so 
  -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMX86Desc -lLLVMX86Info 
  -lLLVMMCDisassembler -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMCodeGen 
  -lLLVMScalarOpts -lLLVMProfileData -lLLVMInstCombine -lLLVMInstrumentation 
  -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMIRReader -lLLVMAsmParser 
  -lLLVMTransformUtils -lLLVMipa -lLLVMMCJIT -lLLVMExecutionEngine -lLLVMTarget 
  -lLLVMAnalysis -lLLVMRuntimeDyld -lLLVMObject -lLLVMMCParser -lLLVMBitReader 
  -lLLVMMC -lLLVMCore -lLLVMSupport -lpapi -lrt -ldl -ltinfo -lpthread -lz -lm 
  -Wl,-rpath,/path-to-gmlos/build/src/FeatureExtractor:/path-to-gmlos/build/src/Support
  </code></pre>