This POC showcases how a Rust application can (re)load plugins by using Dynamic Link Libraries (DLL).
- Rust
- An operating system with support for DLL (e.g. macOS, Windows, Linux, ...)
- Compile all the plugins and the application. Because the application does not depend on any plugins directly, we need to manually build those.
cargo build --release
- Run the aplication. We are using
cargo
to simplify the execution, but it can be done without it. See packaging and distribution for more information.
# Change the extension to match your operating system
# macOS: .dylib
# Windows: .dll
# Linux: .so
cargo run --release -- libhello_world.dylib
Take a look to cargo-dynamic. Please note that some dependencies might be tricky to set up, particuarly those that use global variables. Some examples include tracing
and log
.
Because all the code is dynamically linked, all dynamic dependencies must be included with the resulting application binary. These include all generated under the target
directory, plus some other that live within the Rust installation. you can find these files by running rustc --print sysroot
.
For example, on a macOS x86_64 system, we can run the application without cargo
by using the following command:
DYLD_LIBRARY_PATH="$(rustc --print sysroot)/lib/rustlib/x86_64-apple-darwin/lib:$PWD/target/release" target/release/app libhello_world.dylib
And we can list the exact dependencies of our project requires by running the following command:
dyld_info -dependents target/release/{app,lib{hello-world,app_core}.dylib}
- Speeding up incremental Rust compilation with dylibs: https://robert.kra.hn/posts/2022-09-09-speeding-up-incremental-rust-compilation-with-dylibs/
- Fyrox hot reload demo: https://www.reddit.com/r/rust/comments/1bjae38/media_fyrox_now_supports_hot_reloading_you_can/
- Reddit discussion: https://www.reddit.com/r/rust/comments/1bmqhui/plugins_systems_in_rust_using_dynamic_link/
- More usefull links: https://www.reddit.com/r/rust/comments/1bmqhui/comment/kwgtq9m/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button