基于 unix 的系统中的延迟负载等效 [英] Delay-Load equivalent in unix based systems

查看:15
本文介绍了基于 unix 的系统中的延迟负载等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于 unix 的系统中,延迟负载是什么.

What is the delay load equivalent in unix based system.

我有一个代码 foo.cpp,在使用 gcc 编译时,我将它链接到共享对象(总共三个 .so 文件.).每个.so 文件都有不同的选项.

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 需要 libversion.so./foo -update 需要 libupdate.so

我需要这些库的符号应该只在运行时解析.

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

./foo -v 不应中断,即使 libupdate.so 库不存在.

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

它正在使用延迟加载选项(在 dll 的属性中)在 Windows 中工作.它在 unix 系统中的等价物是什么.

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

-lazy 选项会在 UNIX 中做同样的事情吗?如果是这样,在哪里包含这个选项:在 makefile 中还是在链接器 ld 中?

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

推荐答案

请参阅系统上的 dlopen() 参考.您可以在运行时手动打开库并解析外部符号,而不是在链接时.

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

挖出一个例子:

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;                  
}                                                 

这篇关于基于 unix 的系统中的延迟负载等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆