external linkage of variable (IBM XL C++ on AIX)

Discussion in 'C++' started by Thomas Lenarz, Mar 6, 2006.

  1. Hello,

    please forgive me for posting this if this problem has come up before.
    I could not find any hint with google.

    I am porting a bunch of source-code from SUN-OS to AIX at the moment
    and have the following situation (simplified and renamed for better
    understanding)


    Source a.cc --> compiled and linked into a binary together with the
    shared object underneath

    .....
    AnyType *globalAnyType;
    ....


    Source b.cc --> compiled and linked into a shared object:

    ....
    extern AnyType *globalAnyType;
    ....

    globalAnyType = new AnyType(....); <----- Segment-Violation here


    On SUN the code worked fine. On AIX I get no compile-errors and no
    link errors. So I assume external references are resolved.

    I found out using dbx that the assignment from "new AnyType(...)" to
    globalAnyType causes the Segment-Violation. The external declared
    globalAnyType appears not to be backed up by memory (dbx shows "nil").

    To proove this I replaced globalAnyType by a non-extern-declared
    variable of Type AnyType and....that works.

    Does anyone have got an idea on why external binding of a variable
    does not work in this case? Are there circumstances you can think of?

    Might special compiler/linker options help?

    I have to mention that I port from an old compiler without
    namespace-support to a compiler with namespace-support. I already
    checked if the variables may be in different namespaces because of
    side effects of include files. But I concluded that this would be
    logically impossible.

    Thanks for any idea what I could try.

    Cheers
    Thomas
    Thomas Lenarz, Mar 6, 2006
    #1
    1. Advertising

  2. Thomas Lenarz

    mlimber Guest

    Thomas Lenarz wrote:
    > Hello,
    >
    > please forgive me for posting this if this problem has come up before.
    > I could not find any hint with google.
    >
    > I am porting a bunch of source-code from SUN-OS to AIX at the moment
    > and have the following situation (simplified and renamed for better
    > understanding)
    >
    >
    > Source a.cc --> compiled and linked into a binary together with the
    > shared object underneath
    >
    > .....
    > AnyType *globalAnyType;
    > ....
    >
    >
    > Source b.cc --> compiled and linked into a shared object:
    >
    > ....
    > extern AnyType *globalAnyType;
    > ....
    >
    > globalAnyType = new AnyType(....); <----- Segment-Violation here
    >
    >
    > On SUN the code worked fine. On AIX I get no compile-errors and no
    > link errors. So I assume external references are resolved.
    >
    > I found out using dbx that the assignment from "new AnyType(...)" to
    > globalAnyType causes the Segment-Violation. The external declared
    > globalAnyType appears not to be backed up by memory (dbx shows "nil").
    >
    > To proove this I replaced globalAnyType by a non-extern-declared
    > variable of Type AnyType and....that works.
    >
    > Does anyone have got an idea on why external binding of a variable
    > does not work in this case? Are there circumstances you can think of?
    >
    > Might special compiler/linker options help?
    >
    > I have to mention that I port from an old compiler without
    > namespace-support to a compiler with namespace-support. I already
    > checked if the variables may be in different namespaces because of
    > side effects of include files. But I concluded that this would be
    > logically impossible.
    >
    > Thanks for any idea what I could try.
    >
    > Cheers
    > Thomas


    So the new and the constructor succeeded, but the pointer assignment
    failed? Is there any funky operator overloading related to AnyType? Can
    you look at &globalAnyType (the address of your global) in your
    debugger? Try reversing the two declarations by putting the actual
    variable in b.cc and the extern declaration in a.cc. Now do you have a
    problem with accessing it in the latter?

    I'm guessing that this might be a weird symptom of an initialization
    order fiasco (cf.
    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.12 and
    following), and a different order of initialization allowed the code to
    work on your other compiler but kills it here. Subtle things can happen
    with the order of initialization. See the FAQ for a solution to such
    problems.

    You may also want to ask in a newsgroup related to your compiler (see
    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.9 for some
    ideas).

    Cheers! --M
    mlimber, Mar 6, 2006
    #2
    1. Advertising

  3. On 6 Mar 2006 13:01:45 -0800, "mlimber" <> wrote:

    >So the new and the constructor succeeded, but the pointer assignment
    >failed?

    Yes!

    >Is there any funky operator overloading related to AnyType?

    No!

    >Can
    >you look at &globalAnyType (the address of your global) in your
    >debugger?

    Yes!

    Result is: definition in a.cc &globalAnyType: 47114711 (Can't remember
    the exact number. But it appears to be a proper address.

    extern declaration in bcc &globalAnyType: 0 (zero)

    >Try reversing the two declarations by putting the actual
    >variable in b.cc and the extern declaration in a.cc. Now do you have a
    >problem with accessing it in the latter?

    Unfortunately I haven't had the time to try. But I will!

    >
    >I'm guessing that this might be a weird symptom of an initialization
    >order fiasco (cf.
    >http://www.parashift.com/c -faq-lite/ctors.html#faq-10.12 and
    >following), and a different order of initialization allowed the code to
    >work on your other compiler but kills it here. Subtle things can happen
    >with the order of initialization. See the FAQ for a solution to such
    >problems.
    >
    >You may also want to ask in a newsgroup related to your compiler (see
    >http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.9 for some
    >ideas).


    Thanks a lot for your great hints and recommendations. I didn't know
    the FAQ and about the initialization order fiasco. It's really
    interesting and I will look into this a bit deeper.

    But I'm a bit sceptical wether this is really the problem. In the end
    "new AnyType(...)" is the only Object or Class-Reference involved and
    globalAnyType is just a variable supposed to hold the pointer to the
    AnyType-Instance.

    For me it seems weird that the "extern declared globalAnyType"
    doesn't have the the same address like the "defined globalAnyType"
    after startup and the linker hasn't complained about anything
    unresolved.

    Thanks a lot for your help again. I will post the solution, when I
    (hopfully) find it. :)

    Cheers
    Thomas
    Thomas Lenarz, Mar 7, 2006
    #3
  4. On Mon, 06 Mar 2006 19:59:53 GMT, (Thomas
    Lenarz) wrote:

    >Source a.cc --> compiled and linked into a binary together with the
    > shared object underneath
    >
    > .....
    > AnyType *globalAnyType;
    > ....
    >
    >
    >Source b.cc --> compiled and linked into a shared object:
    >
    > ....
    > extern AnyType *globalAnyType;
    > ....
    >
    > globalAnyType = new AnyType(....); <----- Segment-Violation here
    >
    >
    >Does anyone have got an idea on why external binding of a variable
    >does not work in this case? Are there circumstances you can think of?
    >
    >Might special compiler/linker options help?
    >

    Solution for this problem is:

    There are different link-types on AIX for shared-libraries. In this
    case it is necessary to use "Run-Time-Linking". This means that
    symbolic information is resolved at program-load-time rather than
    link-time.

    This is forced by generating the the library with the exra option -G
    and generating the main-application with the extra option -rtl.

    Using "Default Linking" did not resolve the external variable
    correctly.

    I am sorry. I know it is OT here, but I just wanted to document the
    solution for completeness.

    Thomas
    Thomas Lenarz, Mar 25, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reinhard Schauer
    Replies:
    0
    Views:
    489
    Reinhard Schauer
    Sep 18, 2003
  2. Thanvir
    Replies:
    0
    Views:
    562
    Thanvir
    Apr 12, 2005
  3. Robert Wells
    Replies:
    4
    Views:
    636
    Default User
    Jun 24, 2008
  4. Replies:
    1
    Views:
    578
    Michael DOUBEZ
    Sep 12, 2008
  5. Qu0ll
    Replies:
    42
    Views:
    1,190
    Thufir Hawat
    Apr 13, 2009
Loading...

Share This Page