Delay-Load equivalent in unix based systems

后端 未结 2 922
一整个雨季
一整个雨季 2020-12-17 21:41

What is the delay load equivalent in unix based system.

I have a code foo.cpp, While compiling with gcc I link it to shared objects(tot

2条回答
  •  别那么骄傲
    2020-12-17 22:47

    See the reference on your system for dlopen(). You can manually open libraries and resolve external symbols at runtime rather than at link time.

    Dug out an example:

    int main(int argc, char **argv) {                 
        void *handle=NULL;                                 
        double (*myfunc)(double);                     
        char *err=NULL;                                  
    
        handle = dlopen ("/lib/libm.so.1", RTLD_LAZY);
        if (!handle) {                                
            err=dlerror();
            perror(err);
            exit(1);                                  
        }                                             
    
        myfunc = dlsym(handle, "sin");                
        if ((err = dlerror()) != NULL)  {           
            perror(err);
            exit(1);                                  
        }                                             
    
        printf("sin of 1 is:%f\n", (*myfunc)(1.));              
        dlclose(handle);            
        return 0;                  
    }                                                 
    

提交回复
热议问题