Skip to content

A public domain, single header C/C++ library to dynamically load libraries at runtime.

Notifications You must be signed in to change notification settings

Arcxm/dynamic_load

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

About dynamic_load

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.

Quick Start

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"

Notes for Linux

Please note that for Linux you'll have to link to -ldl.

Documentation

Example

For a full example see example.c and the library it loads: test_lib.c.

dyn_open

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

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

dyn_close closes a previously opened handle to a library.

// [open library and do stuff with it]

dyn_close(lib);

About

A public domain, single header C/C++ library to dynamically load libraries at runtime.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages