Problem: shared object loading runs constructor of a static object, but static linkage does not.

Discussion in 'C++' started by tropos, Nov 29, 2005.

  1. tropos

    tropos Guest

    (Platform: Solaris with gmake and native Sun C++ compiler)

    Problem: If I create a shared object (.so file) and load it into a
    executable, the loader correctly runs constructors of static objects in
    the .so file. But if I link the same code statically, with no shared
    object, then the constructors don't run at all! Why??

    Here's an example:

    <file AnnounceConstruction.cpp>

    #include <iostream> //for cout
    #include <ostream> //needed for cout linkage

    class AnnounceConstruction
    {
    public:
    AnnounceConstruction()
    { std::cout << "AnnounceConstruction constructed."
    << std::endl; }
    };

    //The static object
    AnnounceConstruction _static_AnnounceConstruction_instance;

    <file main.cpp>
    #include <iostream> //for cout
    #include <ostream> //needed for cout linkage

    int main()
    {
    std::cout << "main() has run." << std::endl;
    }

    # Now I compile AnnounceConstruction.cpp into an archive, link
    statically,
    # and show that the constructor of AnnounceConstruction does not run.

    staticdynamic> CC -c AnnounceConstruction.cpp -o
    obj/AnnounceConstruction.o
    staticdynamic> CC -xar -o AnnounceConstruction.a
    obj/AnnounceConstruction.o
    staticdynamic> CC main.cpp -Bstatic AnnounceConstruction.a -o test.out
    staticdynamic> ./test.out
    main() has run.
    staticdynamic>

    # This time, I compile AnnounceConstruction.cpp into a shared object,
    link,
    # and show that the constructor runs correctly.
    staticdynamic> CC -G obj/AnnounceConstruction.o -o
    AnnounceConstruction.so
    staticdynamic> CC main.cpp -L. -Bdynamic AnnounceConstruction.so -o
    test.out
    staticdynamic> export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
    staticdynamic> ./test.out
    AnnounceConstruction constructed.
    main() has run.
    staticdynamic>

    Can anyone explain this?
     
    tropos, Nov 29, 2005
    #1
    1. Advertising

  2. tropos

    Howard Guest

    "tropos" <> wrote in message
    news:...
    > (Platform: Solaris with gmake and native Sun C++ compiler)
    >
    > Problem: If I create a shared object (.so file) and load it into a
    > executable, the loader correctly runs constructors of static objects in
    > the .so file. But if I link the same code statically, with no shared
    > object, then the constructors don't run at all! Why??
    >


    Linking and loading are not issues covered by the C++ language standard, and
    thus are off-topic here. You need to ask in a newsgroup devoted to your
    platform.


    -Howard
     
    Howard, Nov 29, 2005
    #2
    1. Advertising

  3. tropos

    GB Guest

    Re: Problem: shared object loading runs constructor of a staticobject, but static linkage does not.

    tropos wrote:
    > (Platform: Solaris with gmake and native Sun C++ compiler)
    >
    > Problem: If I create a shared object (.so file) and load it into a
    > executable, the loader correctly runs constructors of static objects in
    > the .so file. But if I link the same code statically, with no shared
    > object, then the constructors don't run at all! Why??
    >
    > Here's an example:
    >


    > staticdynamic> CC -c AnnounceConstruction.cpp -o
    > obj/AnnounceConstruction.o
    > staticdynamic> CC -xar -o AnnounceConstruction.a
    > obj/AnnounceConstruction.o
    > staticdynamic> CC main.cpp -Bstatic AnnounceConstruction.a -o test.out
    > staticdynamic> ./test.out
    > main() has run.
    > staticdynamic>
    >


    Your AnnnounceConstruction.o file is not actually getting pulled in from
    the .a file because main.o has no dependencies on it. That's the whole
    point of ar files. You only get what you use. Try compiling by explicity
    listing the .o file on the CC line instead of creating an ar file.

    Gregg
     
    GB, Nov 30, 2005
    #3
  4. tropos

    None Guest

    Re: Problem: shared object loading runs constructor of a static object, but static linkage does not.

    Another option to solve your problem is to pass some command line args
    to the linker to tell it not to be so smart about deciding which
    symbols to import or not. This varies tremendously by platform - for
    example, using gnu ld, you would use the option --whole-archive (I
    think - it's been a while since I've done this. The man page gives a
    good explanation of how to use it.

    -matt
     
    None, Nov 30, 2005
    #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. Douglass Turner
    Replies:
    2
    Views:
    2,043
    Manfred Bartz
    Sep 4, 2003
  2. Ollie
    Replies:
    0
    Views:
    371
    Ollie
    Oct 2, 2003
  3. akennis
    Replies:
    7
    Views:
    765
    Peter
    Jul 26, 2006
  4. Replies:
    1
    Views:
    592
    Michael DOUBEZ
    Sep 12, 2008
  5. Ramon F Herrera
    Replies:
    4
    Views:
    361
    Ian Collins
    Sep 28, 2009
Loading...

Share This Page