dynamic_load is a public domain single header C/C++ library to dynamically load libraries at runtime.
It supports Windows and Linux, using dlopen on Linux and LoadLibrary on Windows.
Just include the header in your project and you're ready to go.
To create the implementation in a file, simply define DYNAMIC_LOAD_IMPLEMENTATION before the #include like this:
#define DYNAMIC_LOAD_IMPLEMENTATION
#include "dynamic_load.h"Please note that for Linux you'll have to link to -ldl.
For a full example see example.c and the library it loads: test_lib.c.
dyn_open opens a library and returns its handle or NULL on error.
DLHANDLE lib = dyn_open("test_lib.so");
if (lib) {
// [do something with it and close the handle afterwards]
} else {
// [error handling]
}dyn_sym looks up and returns the address of a symbol (variable or function) in the library supplied by the handle or NULL if it was not found.
// [open handle]
int *lib_var = dyn_sym(lib, "test_var");
if (lib_var) {
// [do something with it, e.g. print it]
fprintf(stdout, "var is %d\n", *lib_var);
} else {
// [error handling]
}
// HINT: use a typedef for functions
typedef int (*test_add_t)(int, int);
test_add_t lib_add = (test_add_t) dyn_sym(lib, "test_add");
if (lib_add) {
// [call the function and do stuff with it]
} else {
// [error handling]
}dyn_close closes a previously opened handle to a library.
// [open library and do stuff with it]
dyn_close(lib);