Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

c - Delay-Load equivalent in unix based systems

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(totally three .so files are there.). Each of the .so file for different option.

./foo -v needs libversion.so ./foo -update needs libupdate.so

I need the symbol for those libraries should be resolved only at the run time.

./foo -v should not break even if libupdate.so library is not there.

It is working in windows using the delay load option(in properties of dll). What is its equivalent in unix systems.

Will -lazy option does the same in UNIX? If so, where to include this option: in makefile or with linker ld?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
", (*myfunc)(1.));              
    dlclose(handle);            
    return 0;                  
}                                                 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...