471,079 Members | 946 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,079 software developers and data experts.

Problem with UNIX/Linux Shared Libraries and Global variables ...

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 ...
Jun 27 '08 #1
1 2842
On Fri, 20 Jun 2008 18:01:10 -0700 (PDT), vi***********@gmail.com
wrote in comp.lang.c:
I observe a behavior with shared libraries (.so) and global variables
that I cannot understand ...
I'd appreciate if someone can explain the behavior ..
I am sure someone can, in an appropriate group like
news:comp.unix.programmer or news:comp.os.linux.development.apps.

You are off-topic here, the C language does not define the details of
specific tool sets and features like shared libraries.
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)
And even more off-topic here. The C++ language defines ways to link
code to compatible C compilers, the C language does not define linkage
to any other language at all.

But this is more of a platform issue than a C++ one.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Gary Cote | last post: by
1 post views Thread by Peetah_junkmail | last post: by
2 posts views Thread by baileyxia | last post: by
1 post views Thread by nidumolu | last post: by
3 posts views Thread by ankugoe7 | last post: by
4 posts views Thread by stuntgoat | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.