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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • viraj.kadakia@gmail.com

    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 ...
  • Jack Klein

    #2
    Re: Problem with UNIX/Linux Shared Libraries and Global variables ...

    On Fri, 20 Jun 2008 18:01:10 -0700 (PDT), viraj.kadakia@g mail.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.li nux.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.l earn.c-c++

    Comment

    Working...