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 your Cargo.toml file 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 PurposeCharacteristics
cdylibFor external languages / FFI (Python, C, Node.js, etc.).Monolithic binary with unused Rust symbols stripped out. It only exports functions explicitly marked #[no_mangle].
dylibFor 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#

// 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/
Author
YoungHa
Published at
2026-10-15