Skip to content

Instantly share code, notes, and snippets.

@dreiss
Created January 12, 2009 22:57
Show Gist options
  • Save dreiss/46215 to your computer and use it in GitHub Desktop.
Save dreiss/46215 to your computer and use it in GitHub Desktop.
program
*.so
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
void shared_lib_operation();
int main() {
void *handle;
void (*funptr)(void);
char *error;
printf("Main program executing shared library operation.\n");
shared_lib_operation();
printf("Loading plugin without RTLD_DEEPBIND.\n");
handle = dlopen("libplug.so", RTLD_LAZY);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
dlerror();
*(void **)(&funptr) = dlsym(handle, "execute_plugin");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
exit(EXIT_FAILURE);
}
funptr();
dlclose(handle);
printf("Loading plugin with RTLD_DEEPBIND.\n");
handle = dlopen("libplug.so", RTLD_LAZY | RTLD_DEEPBIND);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
dlerror();
*(void **)(&funptr) = dlsym(handle, "execute_plugin");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
exit(EXIT_FAILURE);
}
funptr();
dlclose(handle);
return 0;
}
CFLAGS = -Wall -Wextra
all: program libplug.so libnorm.so libspec.so
program: main_program.c libspec.so
$(CC) -o $@ $< -L. -ldl -lspec
libplug.so: plugin.c libnorm.so
$(CC) -o $@ $< -L. -shared -fPIC -lnorm
libnorm.so: normal_shared_library.c
$(CC) -o $@ $< -shared -fPIC
libspec.so: special_shared_library.c
$(CC) -o $@ $< -shared -fPIC
.PHONY: clean run
run: all
LD_LIBRARY_PATH=$$PWD ./program
clean:
$(RM) program *.so
#include <stdio.h>
void shared_lib_operation() {
printf("Normal Shared Library\n");
}
#include <stdio.h>
void execute_plugin() {
printf("Plugin executing shared library operation.\n");
shared_lib_operation();
}
#include <stdio.h>
void shared_lib_operation() {
printf("Special Shared Library\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment