Created
December 6, 2017 16:17
-
-
Save mbogochow/aa332e6837d0e258e2df691c7685fa05 to your computer and use it in GitHub Desktop.
dlopen demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _POSIX_C_SOURCE 200809L | |
#define _ISOC99_SOURCE | |
#define _XOPEN_SOURCE 700 | |
#include "library.h" | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <dlfcn.h> | |
#define LIB_FILE "liblib.so" | |
#define FUNC_NAME "libfunction" | |
int main(int argc, char **argv) | |
{ | |
int ret; | |
void *lib_handle; | |
int (*func_handle)(void); | |
// Suppress unused warnings | |
(void) argc; | |
(void) argv; | |
// Load the library | |
lib_handle = dlopen(LIB_FILE, RTLD_NOW); | |
if (lib_handle == NULL) | |
{ | |
fprintf(stderr, "Failed to open library %s\n", dlerror()); | |
return EXIT_FAILURE; | |
} | |
// Load the function and assign it to a function pointer so that we can call it | |
// See man page about possible compiler warnings about casting to function pointer | |
func_handle = dlsym(lib_handle, FUNC_NAME); | |
if (func_handle == NULL) | |
{ | |
fprintf(stderr, "Failed to load function %s: %s\n", FUNC_NAME, dlerror()); | |
return EXIT_FAILURE; | |
} | |
// Call the function | |
printf("Library function return value: %d\n", (*func_handle)()); | |
// Close the library | |
ret = dlclose(lib_handle); | |
if (ret != 0) | |
{ | |
fprintf(stderr, "WARNING dlclose failed: %s\n", dlerror()); | |
} | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "library.h" | |
int libfunction() | |
{ | |
return 42; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DLOPEN_DEMO_LIBRARY_H | |
#define DLOPEN_DEMO_LIBRARY_H | |
int libfunction(); | |
#endif //DLOPEN_DEMO_LIBRARY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CC=gcc -std=c99 | |
LD=$(CC) | |
CFLAGS=-Wall -Wextra -Wfloat-equal -Wundef -Wcast-align -Wpointer-arith -Wcast-qual -g -O | |
# -ldl is necessary for dlopen to work and it ensures that the library is dynamically loaded | |
# and not loaded automatically by the application | |
# -Wl,-R. tells the application to look for libraries in the current directory during runtime | |
LFLAGS=-ldl -Wl,-R. | |
DEPS=library.h | |
LIB_NAME=liblib.so | |
APP_NAME=dlopen_demo | |
all: $(APP_NAME) $(LIB_NAME) | |
%.o: %.c $(DEPS) | |
$(CC) -c -o $@ $< $(CFLAGS) $(IFLAGS) -fPIC | |
$(APP_NAME): % : %.o | |
$(LD) -o $@ $^ $(LIBS) $(LFLAGS) | |
$(LIB_NAME): library.o | |
$(LD) -o $@ $^ $(LIBS) $(LFLAGS) -shared | |
clean: | |
rm -rf $(LIB_NAME) $(APP_NAME) *.o | |
.PHONY: all clean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment