Linux 上的 OpenGL:dlopen libGL.so
大多数在 Linux 上使用 OpenGL 的应用程序(和库)在运行时使用 dlopen
API 加载 libGL.so
,而不是动态链接它。
他们为什么要这样做?
我能想象的唯一原因是,这是因为任何图形驱动程序供应商都提供了不同的 libGL
,并且两个不同的 libGL
可能是 ABI 不兼容。 (嗯,嗯,为什么它们应该与 ABI 不兼容?即使它们是,为什么通过 dlopen 加载它们会解决这个问题?)
无论如何,假设有一个很好的理由这样做,我会我也喜欢这样做。有没有人有一个开源 C/C++ 代码的链接,该代码可以通过 dlopen 加载所有 OpenGL 函数,我可以将其包含到我的项目中而不需要太多调整?
Most applications (and libraries) using OpenGL on Linux load libGL.so
at runtime using dlopen
API, instead of dynamically linking against it.
Why do they do this?
The only reason I can imagine is that it's because any graphic driver vendor provides a different libGL
, and two different libGL
could be ABI incompatible. (Well, hum, why should they be ABI incompatible? And even if they are, why loading them via dlopen
would fix this issue?)
Anyway, supposing there's a good reason for doing that, I'd like to do that as well. Does anybody have a link to an opensource C/C++ code that loads all the OpenGL functions via dlopen
, which I can include to my project without needing too many tweaks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
人们这样做有两个主要原因:
There are two main reasons people do this:
这样做是为了让您不必静态链接到 GL 实现,例如,如果您的代码使用 glBindFragDataLocation(可在 OpenGL 3.0 及更高版本上使用),则它将无法在 OpenGL 2.1 及更早版本上运行,并出现神秘的链接器错误实施。
因此,动态获取入口点允许您在运行时选择适当的渲染路径。
此外,在 Windows 上,GL 函数需要它 > 1.1.
GLEW 会为您完成此操作,它不会 dlopen libGL,它使用 glXGetProcAddress/wglGetProcAddress/aglGetProcAddress 从驱动程序获取 GL 函数指针,并且它是跨平台的。
This is made so you don't have to statically link to a GL implementation, for example, if your code uses glBindFragDataLocation, which is available on OpenGL 3.0 and newer, it would fail to run with a cryptic linker error on OpenGL 2.1 and earlier implementations.
So getting entry points dynamically allows you to select the apropiate rendering path at runtime.
Also, it's required on Windows for GL functions > 1.1.
GLEW does this for you, it doesn't dlopen libGL, it uses glXGetProcAddress/wglGetProcAddress/aglGetProcAddress to get GL function pointers from the driver, and it's cross platform.