dlopen与标准动态链接的用例是什么? [英] What are the use cases of dlopen vs standard dynamic linking?

查看:130
本文介绍了dlopen与标准动态链接的用例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档, dlopen dlsym 来加载库,并获得指向符号的指针。

According to the doc, dlopen is used in conjunction with dlsym to load a library, and get a pointer to a symbol.

但是,这已经是动态加载程序/链接程序的工作。
而且,这两种方法都基于 ld.so

But that's already what the dynamic loader/linker does. Moreover, both methods are based on ld.so.

使用 dlopen 似乎实际上有两个区别:

There actually seems to be two differences when using dlopen:


  1. 可以有条件地加载该库。

  2. 编译器不知道我们使用的符号(类型,原型...),因此不会检查潜在的错误。

但是,它似乎并没有激发使用 dlopen 超过标准加载,除了边际示例:

But, it does not seem to motivate the use of dlopen over standard loading, except for marginal examples:


  1. 条件加载实际上并不是很有趣共享库已被另一个程序使用的情况下优化内存占用量的方法:加载已使用的库并不会增加内存占用量。编写错误...我们还缺少潜在的编译器优化。

因此,还有其他用途,其中 dlopen ?

So, are there other uses where dlopen is prefered over the standard dynamic linking/loading?

推荐答案


那么,还有其他用途比标准动态链接/加载更喜欢dlopen吗?

So, are there other uses where dlopen is prefered over the standard dynamic linking/loading?

使用<$的典型用例c $ c> dlopen 是


  • 插件

  • 选择当前CPU的最佳实现方式(英特尔数学库可以这样做)

  • 选择不同供应商的API实现(GLEW

  • 如果不太可能使用共享库,则延迟加载(这将加快启动速度,因为库构造函数无法运行,而运行时链接程序的工作量会稍少)

  • plugins
  • selecting optimal implementation for current CPU (Intel math libraries do this)
  • select implementation of API by different vendors (GLEW and other OpenGL wrappers do this)
  • delay loading of shared library if it's unlikely to be used (this would speed up startup because library constructors won't run + runtime linker would have slightly less work to do)

避免编译器监督是不安全的,也是编写错误的好方法...
我们还缺少潜在的编译器优化。

Avoiding the compiler supervision is unsafe and a good way to write bugs... We're also missing the potential compiler optimizations.

是的,但是您可以通过提供一些延迟加载共享库周围的包装器库。在Windows上,这是通过标准工具( DLL导入库的谷歌工具)完成的,在Linux上,您可以手动完成此操作,也可以使用 Implib.so

That's true but you can have best of both worlds by providing a small wrapper library around delay loaded shared library. On Windows this is done by standard tools (google for "DLL import libraries"), on Linux you can do it by hand or use Implib.so.

这篇关于dlopen与标准动态链接的用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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