1

I am curious about whether static variables instantiated inside shared libraries are instantiated at all. First of all, here is the code:

AClass.h

class A {
  public:
    A(int x) { cout << "A constructor called." << endl; }
};

MyLib.cpp

#include "AClass.h"

static A StaticVariableA(50);

So, with this code plus a Main.cpp file (with nothing but a empty main function), I wanted to get the constructor to be called by the initialization of the variable inside MyLib.cpp.

I compiled MyLib.cpp 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:

(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

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)

(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)

Strangely enough, this code works. When linking the library GMLOSStFeatEx, which is a shared library compiled with /src/FeatureExtractor/ files, to the /tools/Dummy.cpp. It calls the constructor of static variables of type RegisterStaticFeature.

EDIT

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.

In this question it suggests the use of the linker flag -no-as-needed, which works. But as it is possible to see below, there is no such flag when compiling the code I mentioned.

/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
Community
  • 1
  • 1
yZaph
  • 169
  • 1
  • 10
  • 1
    What is a `static global` variable? It's either static or it's global. Also, if this is in the cpp file: `static A StaticVariableA(50);` That variable is only known within that module, no other. It is *not* a global variable. – PaulMcKenzie Mar 09 '16 at 03:50
  • Oh yeah. Sorry about that. Indeed it can not be static and global at the same time. It is about static variables. – yZaph Mar 09 '16 at 04:23
  • Maybe your MyLib.cpp was optimized out by the compiler since no references found to that library? Try to add some dummy function in the MyLib.cpp and call it from main.cpp – Pupsik Mar 09 '16 at 04:52
  • @Pupsik This seem to be the case. But it is odd. Why does [this](https://github.com/YuKill/gmlos/blob/master/src/FeatureExtractor/DefInstructionFeature.cpp) don't get optimized out? It doesn't have any functions referenced outside. – yZaph Mar 09 '16 at 05:12

1 Answers1

0

So, it turns out that @Pupsik was completely right! Btw, thanks.

As he said, the library was being optimized out:

Maybe your MyLib.cpp was optimized out by the compiler since no references found to that library? Try to add some dummy function in the MyLib.cpp and call it from main.cpp

The code on github that I referenced didn't get optimized out because there was, indeed, a function inside that library being called by the main function.

The solution I found was the use of the linker flag -as-needed. It links the library no matter what, thus not being taken out of the binary.

So, that clears things out. Thanks, again.

yZaph
  • 169
  • 1
  • 10