I observe a behavior with shared libraries (.so) and global variables
that I cannot understand ...
I'd appreciate if someone can explain the behavior ..
Scenario 1:
aTest is an executable on Solaris/Linux built from main.o (main.cpp)
bTest dynamically loads a shared libraires mylib1.so and mylib2.so
both built from mylibsrc.o (mylibsrc.cpp)
cmylibsrc.cpp contains a global variable 'int MyGlobal' that is non
static and not extern (simply int MyGlobal)
dI have extern C Set and Get functions in mylibsrc.cpp to access
MyGlobal
eUnder these circumstatnces, when I run Test, MyGlobal seems to have
two instances in memory, one for each .so
Their runtime values (modified by dlsym(handle, Set)) and
addresses are different as expected.
Scenario 2:
aEverything is same as Scenario 1 except ..
bmain.cpp (Test) also has a global non-static, non-extern variable
'int MyGlobal'
cNow when I run Test, all 3 MyGlobal(s) (in Test, mylib1.so and
mylib2.so) are the same memory address and same value.
Why is the behavior under these to scenarios different ?
I expect that MyGlobal in main.cpp should have its own instance ...
p.s. I use -fPIC / -KPIC and -Bsymbolic while generating the .so(s)
Thanks in advance ...