#shared #shared-memory #api-bindings #dlopen #load #dylib #memory-safe

libloading

Bindings around the platform's dynamic library loading primitives with greatly improved memory safety

36 releases

Uses old Rust 2015

0.8.6 Nov 29, 2024
0.8.5 Jul 20, 2024
0.8.4 Jun 23, 2024
0.8.3 Mar 5, 2024
0.2.0 Nov 8, 2015

#8 in Operating systems

Download history 1954695/week @ 2024-08-24 1517028/week @ 2024-08-31 1739776/week @ 2024-09-07 1403989/week @ 2024-09-14 1822574/week @ 2024-09-21 1958529/week @ 2024-09-28 2501034/week @ 2024-10-05 2579155/week @ 2024-10-12 1960793/week @ 2024-10-19 1470240/week @ 2024-10-26 1650640/week @ 2024-11-02 1613189/week @ 2024-11-09 1744146/week @ 2024-11-16 1325318/week @ 2024-11-23 1782101/week @ 2024-11-30 1860330/week @ 2024-12-07

6,998,078 downloads per month
Used in 7,346 crates (499 directly)

ISC license

95KB
973 lines

Contains (Windows DLL, 4KB) tests/nagisa32.dll, (Windows DLL, 3KB) tests/nagisa64.dll

libloading

Bindings around the platform's dynamic library loading primitives with greatly improved memory safety. The most important safety guarantee of this library is the prevention of dangling Symbols that may occur after a Library is unloaded.

Using this library allows the loading of dynamic libraries, also known as shared libraries, as well as the use of the functions and static variables that these libraries may contain.

libloading is available to use under ISC (MIT-like) license.


lib.rs:

Bindings around the platform's dynamic library loading primitives with greatly improved memory safety.

Using this library allows the loading of dynamic libraries, also known as shared libraries, and the use of the functions and static variables they contain.

The libloading crate exposes a cross-platform interface to load a library and make use of its contents, but little is done to hide the differences in behaviour between platforms. The API documentation strives to document such differences as much as possible.

Platform-specific APIs are also available in the os module. These APIs are more flexible, but less safe.

Installation

Add the libloading library to your dependencies in Cargo.toml:

[dependencies]
libloading = "0.8"

Usage

In your code, run the following:

fn call_dynamic() -> Result<u32, Box<dyn std::error::Error>> {
    unsafe {
        let lib = libloading::Library::new("/path/to/liblibrary.so")?;
        let func: libloading::Symbol<unsafe extern fn() -> u32> = lib.get(b"my_func")?;
        Ok(func())
    }
}

The compiler will ensure that the loaded function will not outlive the Library from which it comes, preventing the most common memory-safety issues.

Dependencies

~0–4.5MB