Skip to content

karnkaul/dino

Repository files navigation

dino

A simple, multi-platform dynamic loader written in C++20.

Build status

Requirements

  • C++20 compiler and standard library.
  • CMake 3.22+.

Usage

Link to the dino::dino target in CMake.

#include <dino/library.hpp> // include the header.

// ...
auto const path = dino::Location{
  .identifier = "lib-name", // platform-agnostic identifier of library
                            // (for libfoo.so/foo.dll the id is foo).
  .directory = "./"         // directory to use; leave empty to
                            // rely on the default search order.
}.to_path();                // build a path using the default 
                            // Affix for this platform.

// load the library.
auto const library = dino::Library{path};
// use dino::Library::make(path) to avoid exceptions on failure.

// load a function reference.
// the signature must match what's in the library source!
// otherwise, calling the function invokes undefined behavior.
auto const func = library.load<int(char)>("foobar"); 
// use library.load_ptr() to get a function pointer instead.

std::cout << func('x'); // call function and print returned int.

CMake build options

  • DINO_BUILD_TESTS: Set ON to build tests (OFF by default unless dino is the project root)
  • DINO_BACKEND: Set to 'unix' or 'windows' to force a particular backend. Default value is 'default' which uses the host platform as detected by CMake.

FAQ

Are the loaded functions actually type-safe?

Unfortunately not, both dlsym() and GetProcAddress() operate on symbol names only.

What happens if the names match but signatures don't?

Undefined behaviour.

Contributing

Contributions are welcome.

Original repository

LICENCE

About

C++20 Library for dynamically loading shared libraries and functions.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published