Find the answer to your Linux question:
Results 1 to 8 of 8
To My knowledge, the initialization order of shared libraries should be decided by their dependency relationship. for example, a program links to libA, libB and libC. libA uses something in ...
  1. #1
    Just Joined!
    Join Date
    Dec 2009
    Posts
    4

    initialization order of libraries

    To My knowledge, the initialization order of shared libraries should be decided by their dependency relationship.

    for example, a program links to libA, libB and libC. libA uses something in libB, and libB uses something in libC. so the initialization orders would be libC, libB and then libA.

    in other words. all global objects in libC must be initialized before all global objects in libB.

    however, one of my applications brokes the above rule. Global objects in libA constructs first.

    I am wondering if there is the so called cyclic dependency problems on linux platform. on solaris platform, sometimes, you got cyclic dependency which causes initializtion order problems.

    if the anwer is yes, is there a way to find what causes the cyclic dependency?
    I used ldd -d, but it seems it can't search libs in correct path.

    thanks in advance.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    This is one of the more gnarly problems with complex applications. There are techinques that will help force initialization in the proper sequence. However, these usually work best with C++ since you can call a function to initialize a variable in a header file, forcing that translation unit into scope at the proper time. Generally, using shared libraries bypasses this problem since they will load shared libraries they have dependencies on before their initializers are called. I take it that you are trying to use statically linked libraries?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Dec 2009
    Posts
    4
    Quote Originally Posted by Rubberman View Post
    This is one of the more gnarly problems with complex applications. There are techinques that will help force initialization in the proper sequence. However, these usually work best with C++ since you can call a function to initialize a variable in a header file, forcing that translation unit into scope at the proper time. Generally, using shared libraries bypasses this problem since they will load shared libraries they have dependencies on before their initializers are called. I take it that you are trying to use statically linked libraries?
    I am using shared libraries and I am also using c++. as far as I know, libraries should be loaded based on their dependency relationship on linux.

    but in my application, libraries are loaded in an incorrect order. all my shared libraries definitely uses stl. but the std::cout object initilized after one of my libraries, in other words, if I call std::cout in a global object of the library, I got a coredump.

    something must be wrong here, I am wondering what may cause this problem.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    I developed a very robust distributed transaction processing framework in C++ for very large scale, high availability manufacturing systems that run most semiconductor fabs world-wide today. This was a problem that we had to resolve, and did it by forcing the order of resolution of these dependencies. Typically that was done by initializing some module or global variable with a function call that would force the linker to pull in and initialize the appropriate libraries before their services were called in the offending module's initializer - functions that are called before main(). This is where you are getting hosed. With C++, unlike C, a lot of stuff goes on before you get to main(), and this is where you have a problem. With complex applications this is sometimes necessary in order to be sure that things are properly initialized before you get to main() so that you don't have unresolved dependencies lurking in the background. However, the downside is what you have discovered.

    So, somewhere in your code, you are making a call to std::cout in the initialization phase before main(), but the iostream libraries have not yet been pulled in and initialized. You need to analyze your code and either eliminate those instances, or you need to force the initialization of std::cout in the manner I have suggested.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Dec 2009
    Posts
    4
    could you please give me some tips on how to force the initialization of std::cout?

    for example, I have this class A as below.defining a gobal object of A would cause a coredump as std::cout has not been initialized when the global object of A is constructed.

    class A
    {
    public:
    A(){std::cout << "A constructor" << std::endl;}
    }



    should I add something like dlopen in A's constructor to load stl ? is this what you suggested? thanks.

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Somewhere you are instantiating an instance of class A before main() is called. In your case, it may be that if you create the body of the constructor out-of-line (not in the header inline, but in the A.cpp file) the problem may resolve itself.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Anyway, as it's midnight on Christmas here, I am heading to bed and visions of sugarplums until Santa gets here tomorrow morning. Any further discussion of this on my part will have to wait until sometime tomorrow... Have a great Christmas, if you celebrate it. If not, have a great day!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Just Joined!
    Join Date
    Dec 2009
    Posts
    4
    merry christmas to you. we don't celeberate this holiday. but we got a pretty strong christmas atmosphere around. it's nice to have Santa

    problem is not caused by writing body of constructor in header file. I tried dlopen(libstlport.so.5.1) before std::cout, it works fine though it seems not a decent solution.

    wondering if there any better solution to force the initilization order of libs.

    anyway, have a nice dream and enjoying your christmas holiday.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...