OpenGL on Linux: dlopen libGL.so
Asked Answered
B

2

11

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?

Brighton answered 15/11, 2010 at 16:9 Comment(1)
Most applications (and libraries) using OpenGL on Linux load libGL.so at runtime using dlopen”, that statement isn't all that true, especially for open source GL-only games.Seamount
C
9

There are two main reasons people do this:

  1. You can give a sensible error for systems that don't have OpenGL
  2. Vendors offer many different extensions and the only sane way to support multiple sets of extensions without different binaries per vendor is to use dlsym to check for them. GLEW offers a nice way of doing this for you though.
Coseismal answered 15/11, 2010 at 16:11 Comment(2)
So why don't do they do the same on other platforms?Brighton
On windows for example WGL provides a mechanism for getting function pointers for extensions, which has to be used for all extensions: opengl.org/wiki/Platform_specifics:_Windows#wglGetProcAddress that's basically the same as calling dlsym, just without the call to dlopenCoseismal
H
8

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.

Hume answered 15/11, 2010 at 20:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.