-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Segfault when calling function from a shared object library #151
Comments
Also, thanks for making glad! It's been quite nice to use. |
Hey, finally got time to look into this. The problem here is, lib also doesn't work with glew, it seems to work because it links against
and
I added some additional printf's in your source files: printf("[lib] glClearColor: %p@%p\n", glClearColor, &glClearColor);
printf("[lib] glClear: %p@%p\n", glClear, &glClear);
printf("[lib] glFramebufferTexture: %p@%p\n", glFramebufferTexture, &glFramebufferTexture);
#ifdef USE_GLEW
printf("[glew] glClearColor: %p@%p\n", __glewClearColorx, &__glewClearColorx);
#endif
As you can see You can work around this by initializing OpenGL (glad or glew) in your library: void lib_init()
{
#ifndef USE_GLEW
gladLoadGL();
#endif
} // Dynamically load a function from lib.so
void *lib_code = dlopen("./lib.so", RTLD_LAZY);
draw_blue_screen_func *draw_blue_screen = (draw_blue_screen_func *) dlsym(lib_code, "draw_blue_screen");
((void(*)()) dlsym(lib_code, "lib_init"))(); I don't know if you can do some tricks and force the library to use the already loaded symbols of your application, it may work if you dynamically link against glad/glew as a dynamic library in both files ( I hope that answers your question. |
Thanks for taking a look at this. After seeing working solution to my example, I ended up investigating and learning a lot more about just what glad is doing when I call |
I'm using glad in a file that get's compiled as a shared object library. When
I dynamically load a function from that library and call it, I get a segfault.
This segfault occurs only when the dynamically loaded function contains calls to
OpenGL functions that have been loaded by glad. If I use Glew, rather than glad,
in the library, the program works as expected.
Example
I have tried to make a reasonably minimal example that reproduces the problem.
A glfw-provided window is opened, a function is dynamically loaded from
lib.so
withdlopen()
anddlsym()
, and that function is called. Thefunction just clears the screen to blue.
When glad is used, a segfault is thrown during the call to
glClearColor()
.Steps to Reproduce
$ chmod +x build.sh && ./build.sh && ./main
build.sh
$ .build.sh && ./main
Structure
build.sh
src/glad.c
src/lib.c
src/main.c
The text was updated successfully, but these errors were encountered: