Use Library::new to find and load a dynamic library from a filename, absolute path, or relative path.
Safety
This function is unsafe because:
- Loading a library executes its initialization routines, which are treated as calling unknown foreign functions.
- The caller must ensure that the execution of termination routines (executed when the library is unloaded) is safe.
Thread Safety
While the implementation strives for MT-safety, certain platforms have limitations (e.g., dlerror on some UNIX targets may not be MT-safe). Additionally, calling this function from multiple threads is not MT-safe if the library search path is being modified (e.g., via SetDllDirectory on Windows or LD_LIBRARY_PATH on UNIX).
Tips
- To improve portability, distribute libraries under a common filename (e.g.,
awesome.module) to avoid platform-specific extensions. - Use absolute or relative paths whenever possible to avoid flakiness caused by platform-dependent search locations.
# use ::libloading::Library;
// Any of the following are valid.
unsafe {
let _ = Library::new("/path/to/awesome.module").unwrap();
let _ = Library::new("../awesome.module").unwrap();
let _ = Library::new("libsomelib.so.1").unwrap();
}