libloading/
lib.rs

1//! Bindings around the platform's dynamic library loading primitives with greatly improved memory safety.
2//!
3//! Using this library allows the loading of [dynamic libraries](struct.Library.html), also known as
4//! shared libraries, and the use of the functions and static variables they contain.
5//!
6//! The `libloading` crate exposes a cross-platform interface to load a library and make use of its
7//! contents, but little is done to hide the differences in behaviour between platforms.
8//! The API documentation strives to document such differences as much as possible.
9//!
10//! Platform-specific APIs are also available in the [`os`] module. These APIs are more
11//! flexible, but less safe.
12//!
13//! # Installation
14//!
15//! Add the `libloading` library to your dependencies in `Cargo.toml`:
16//!
17//! ```toml
18//! [dependencies]
19//! libloading = "0.8"
20//! ```
21//!
22//! # Usage
23//!
24//! In your code, run the following:
25//!
26//! ```no_run
27//! fn call_dynamic() -> Result<u32, Box<dyn std::error::Error>> {
28//!     unsafe {
29//!         let lib = libloading::Library::new("/path/to/liblibrary.so")?;
30//!         let func: libloading::Symbol<unsafe extern "C" fn() -> u32> = lib.get(b"my_func")?;
31//!         Ok(func())
32//!     }
33//! }
34//! ```
35//!
36//! The compiler will ensure that the loaded function will not outlive the `Library` from which it comes,
37//! preventing the most common memory-safety issues.
38#![cfg_attr(
39    any(unix, windows),
40    deny(missing_docs, clippy::all, unreachable_pub, unused)
41)]
42#![cfg_attr(libloading_docs, feature(doc_cfg))]
43#![no_std]
44
45extern crate alloc;
46#[cfg(feature = "std")]
47extern crate std;
48
49mod as_filename;
50mod as_symbol_name;
51
52pub use as_filename::AsFilename;
53pub use as_symbol_name::AsSymbolName;
54
55pub mod changelog;
56mod error;
57pub mod os;
58#[cfg(any(unix, windows, libloading_docs))]
59mod safe;
60mod util;
61
62pub use self::error::Error;
63
64#[cfg(any(unix, windows, libloading_docs))]
65pub use self::safe::{Library, Symbol};
66
67/// Converts a library name to a filename generally appropriate for use on the system.
68///
69/// This function will prepend prefixes (such as `lib`) and suffixes (such as `.so`) to the library
70/// `name` to construct the filename.
71///
72/// # Examples
73///
74/// It can be used to load global libraries in a platform independent manner:
75///
76/// ```
77/// use libloading::{Library, library_filename};
78/// // Will attempt to load `libLLVM.so` on Linux, `libLLVM.dylib` on macOS and `LLVM.dll` on
79/// // Windows.
80/// let library = unsafe {
81///     Library::new(library_filename("LLVM"))
82/// };
83/// ```
84#[cfg(feature = "std")]
85#[cfg_attr(libloading_docs, doc(cfg(feature = "std")))]
86pub fn library_filename<S: AsRef<std::ffi::OsStr>>(name: S) -> std::ffi::OsString {
87    use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
88
89    let name = name.as_ref();
90    let mut string =
91        std::ffi::OsString::with_capacity(name.len() + DLL_PREFIX.len() + DLL_SUFFIX.len());
92    string.push(DLL_PREFIX);
93    string.push(name);
94    string.push(DLL_SUFFIX);
95    string
96}