Calling & Building / FFI & Export

FFI & Export

extern "C": Calling C Functions

extern "C" fn strlen(s: str) -> i64;

print(strlen("hello"));   // 5

Declare an external C function and call it directly.

#[export]: Exporting C ABI Symbols

Turn a function into a C ABI symbol visible in a dynamic library, combined with --shared:

#[export]
fn add(a: i64, b: i64) -> i64 {
    return a + b;
}

Constraints

No genericsmust be monomorphized
Cannot be mixed with extern "C"—
Parameter restrictionsi32 / i64 / f64 / bool / str / String / *T
Return valuesame as above, or no return

#[py_export]: Exporting Python

On top of #[export] it auto-generates a CPython glue layer; see Exporting Python Modules for details.

Linking External Libraries

[dependencies]
sqlite = { path = "../deps/aero-sqlite" }

[link]
lib_paths = ["../deps/aero-sqlite"]
libs = ["sqlite3"]

Use relative paths for FFI dependency paths to keep them portable across machines. aero-sqlite bundles libsqlite3.a, so it works right after install without a C toolchain.

Aero 文档 · 基于 Aero 1.2.0 文档整理 · MIT
SereinTeam