-1

I have following code

  1. File hello.cc

    static A dummyl;
    
    A:: A() {
        fun();
    }
    
    void A::fun() {
        int y = 10;
        int z = 20; 
        int x = y + z;
    }   
    
  2. File hello.h

    class A {
      public:
        A a;
        void fun();
    };
    
  3. File main.cc

    #include <dlfcn.h>
    #include "hello.h"
    
    typedef void (*pf)();
    int main() {
        void *lib;
        pf greet;
        const char * err;
        printf("\n Before dlopen\n");
        lib = dlopen("libhello.so", RTLD_NOW | RTLD_GLOBAL);
        if (!lib) {
            exit(1);
        }
        A *a = new A ;
        a->fun();
        dlerror(); /*first clear any previous error; redundant in this case but a useful habit*/
        dlclose(lib); 
        return 0;
    }
    

Build phases:

  1. g++ -fPIC -c hello.cc
  2. g++ -shared -o libhello.so hello.o
  3. g++ -o myprog main.cc -ldl -L. -lhello

Since my shared library in real case is libQtCore.so , I need to link it as using -lQtCore in linker because I cannot use the symbols directly and since there are many of functions then libQtCore, it will not practically advisable to use dlysym for each function of libQtCore.so

Since I link, my static global variables gets initialized before dlopen. Is there any flag for linker g++ that tells compiler to load the shared library only after _dlopen _?

CristiFati
  • 38,250
  • 9
  • 50
  • 87
TechEnthusiast
  • 273
  • 4
  • 18
  • 5
    You are missing some fundamental concepts. Once the code is compiled, the compiler is completely out of the picture. It's not the compiler that "load(s) the shared library", of a program at runtime. Furthermore, a library is either linked or not linked to an executable. If it's linked, it gets loaded at runtime. If not, it does not, and it can be loaded with dlopen(). The End. The concept of "not loading the library until dlopen" makes no sense. That's what dlopen() does, exactly: it loads a library that has not been directly linked to the executable. – Sam Varshavchik Sep 14 '16 at 23:28
  • could you point me documentation where I can see the exact difference loading shared library at runtime using dlopen or linking the shared library – TechEnthusiast Sep 15 '16 at 00:06
  • [Here](http://stackoverflow.com/questions/1993390/static-linking-vs-dynamic-linking)'s something that might help. – CristiFati Sep 15 '16 at 00:20
  • I found the explanation [here](https://en.wikipedia.org/wiki/Dynamic_loading) quite helpful. – Robert Prévost Sep 15 '16 at 00:22

1 Answers1

0

Is there any flag for linker g++ that tells compiler to load the shared library only after _dlopen _?

Yes. On Windows. It's known as delay loading. It doesn't exist on Linux/OS X. It's possible to implement it, but it'd require modifications to binutils and ld.so. See this article for some background on that.

But you don't need to care about any of it. Really.

You are facing a well known problem. So well known that it even has a name: The Static Initialization Order Fiasco.

The solution to all your woes is trivial: don't use static global variables the way you do.

To fix it, use Q_GLOBAL_STATIC, or reimplement something like it yourself. That way the value will be constructed at the time it's first used, not prematurely. That's all there's to it.

Side note: Your recent questions suffer badly by being an XY Problem. You're trying to come up with all sorts of Rube Goldberg-esque solutions to a rather simple issue that it took you a week+ to finally divulge. Ask not about a potential solution, but about the underlying issue is you're attempting to solve. All the library loading stuff is completely and utterly unrelated to your problem, and you don't need to concern yourself with it at all.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313