Categories
Tags
3d ABI Actions algorithms alignment analyze APIT Arc Architecture arm array ascii ash assembly asynchronous attribute base64 bithacks Blogging box c c23 cdylib CHAR_BIT clamp clang clang-format client clippy cmake compiler Computer concat concurrency const_fn constexpr contravariant cos covariant cpp cpu crate CS Customization cybersecurity DataStructure db debugging Demo deserialization discrete dll doc DP drawio dtruss dylib Dynamic emulator example Example FFI ffmpeg flamegraph flat_map fold format FP fsanitize Functional FunctionalProgramming functions futures Fuwari game GATs gcc gccrs generics gitignore glibc GUI hacking hashmap haskell heap hyperfine Imperative inference intel interop invariant iterator join justfile kernel LaTeX leak LFU limits linux lto MachineLearning macOS map Markdown math ML mmap mod mp3 nc nextest OnceLock optimization OS ownership panic parallels perf physics pin postgresql procedure product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift SIMD sin size SmallProjects so socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous syscall tan test thread time toml tracing traits trap triangulation type uint32_t UnsafeRust utf16 utf8 vector Video vulkan wsl x86_64 xilem zig
364 words
2 minutes
cdylib_ABI_dylib
link
cdylib
A cdylib (C-interface dynamic library) is a target type in Rust used to compile your project into a shared dynamic library designed to be loaded by other languages or external runtimes (such as C, C++, Python, Node.js, or Java via JNI).
- When you build a cdylib, the Rust compiler strips out Rust-specific metadata and outputs a standard system dynamic library:
When you build a cdylib, the Rust compiler strips out Rust-specific metadata and outputs a standard system dynamic library:
• Linux:
.so• macOS:
.dylib• Windows:
.dll
How to Configure cdylib in Cargo
- To configure your Rust library to compile as a
cdylib, add the crate-type array to yourCargo.tomlfile under the[lib]section:
[package]
name = "my_ffi_library"
version = "0.1.0"
edition = "2021"
[lib]
# "cdylib" produces the C-compatible dynamic library.
# "rlib" is optionally included so you can still use it as a normal Rust dependency.
crate-type = ["cdylib", "rlib"]Basic Implementation Example
When writing code for a
cdylib, you generally expose functions using the C calling convention (extern "C") and prevent Rust from mangling the function names (#[no_mangle]):src/lib.rs:
#[no_mangle]
pub extern "C" fn add_numbers(a: i32, b: i32) -> i32 {
a + b
}Building the Project
- Compile the library using the standard Cargo command:
cargo build --release- After compilation, your compiled shared library file will be available in the target directory:
- •
target/release/libmy_ffi_library.so(Linux) - •
target/release/libmy_ffi_library.dylib(macOS) - •
target/release/my_ffi_library.dll(Windows)
- •
cdylib vs dylib
| Library Type | Primary Purpose | Characteristics |
|---|---|---|
cdylib | For external languages / FFI (Python, C, Node.js, etc.). | Monolithic binary with unused Rust symbols stripped out. It only exports functions explicitly marked #[no_mangle]. |
dylib | For Rust-to-Rust dynamic linking. | Retains internal Rust compiler metadata and layout specifics, meaning it expects the exact same compiler version for dependencies. |
Advanced: Passing Linker Arguments
- If your dynamic library requires custom linker flags (such as setting an explicit
sonameon Linux or managing symbols), you can targetcdylibtargets specifically inside abuild.rsscript using thecargo::rustc-link-arg-cdylibinstruction:
// build.rs
fn main() {
// This flag is only applied when building the cdylib artifact
println!("cargo::rustc-link-arg-cdylib=-Wl,-rpath,@loader_path");
}- If you are trying to implement something specific, let me know:
- • What language or tool will be consuming this library?
- • Do you need assistance with complex data types (like strings or structs) across the FFI boundary?
cdylib_ABI_dylib
https://younghakim7.github.io/blog/posts/cdylib_abi_dylib/