Note
The API is documented here https://lief.re/doc/stable/rust/lief and the nightly doc is here: https://lief-rs.s3.fr-par.scw.cloud/doc/latest/lief/index.html.
[package]
name = "my-awesome-project"
version = "0.0.1"
edition = "2024"
[dependencies]
lief = { git = "https://github.com/lief-project/LIEF", branch = "main" }
Warning
LIEF rust bindings are not on docs.rs because of network restrictions: https://github.com/rust-lang/docs.rs/issues/2563
LIEF’s Rust bindings are split into two parts:
lief: the high-level, idiomatic Rust API.
lief-ffi: the low-level FFI API based on cxx.
Two additional crates support the build process:
lief-build: build-script helper used by lief-ffi to fetch the pre-compiled artifacts and emit the cargo link directives.
lief-ffigen: standalone CLI that generates the C++ side of the cxx bridge from the #[cxx::bridge] modules declared in lief-ffi.
Building lief-ffi requires generating the C++ bridge files with lief-ffigen and then compiling them alongside libLIEF. Both steps can take several minutes.
To save time, LIEF provides pre-compiled versions of these artifacts, which are downloaded from GitHub (for releases) or an S3 bucket (for nightly builds). If you need to avoid downloading these pre-compiled files, set This variable can also be used when building in offline mode (e.g., As of now, the following targets are supported with pre-compilation: Target Description Regular Linux x86-64 (Ubuntu 19.10, Debian 10, …) Regular Linux i686 (Ubuntu 19.10, Debian 10, …) Musl target that allows full static build Linux i686 with Musl Linux aarch64 (Debian 12+) Linux aarch64 with Musl Android aarch64 (API 30+) Android x86_64 (API 30+) macOS 11+ x86-64 macOS 11+ arm64 (Apple Silicon) iOS 12+ Regular Windows x86-64 (static UCRT runtime) Regular Windows x86-64 (dynamic UCRT runtime Regular Windows arm64 (static UCRT runtime) Regular Windows arm64 (dynamic UCRT runtime The assets of the pre-compiled output are: LIEF static library: The LIEF static library must be compiled as described in the Compilation section using the CMake option: Then compile them as a regular CMake-based library:LIEF_RUST_PRECOMPILED¶LIEF_RUST_PRECOMPILED to point to the directory that contains these files:/home/romain/out
└── lib
├── libLIEF.a
└── liblief-sys.a
1 directory, 2 files
LIEF_RUST_PRECOMPILED=/home/romain/out cargo build [...]
cargo --offline).x86_64-unknown-linux-gnui686-unknown-linux-gnux86_64-unknown-linux-musli686-unknown-linux-muslaarch64-unknown-linux-gnuaarch64-unknown-linux-muslaarch64-linux-androidx86_64-linux-androidx86_64-apple-darwinaarch64-apple-darwinaarch64-apple-iosx86_64-pc-windows-msvc[MT]x86_64-pc-windows-msvc[MD].dll)aarch64-pc-windows-msvc[MT]aarch64-pc-windows-msvc[MD].dll)Precompilation¶
LIEF.{a,lib}liefsys.{a,lib}: bridge between C++ and Rust-DLIEF_RUST_API=ON.liefsys.{a,lib} is built in two steps. First, generate the C++ bridge files with lief-ffigen:$ cargo build [--profile release] -p lief-ffigen
$ target/{release,debug}/lief-ffigen \
--output-dir cxx-bridge/ \
--source-dir lief-ffi/
$ cmake -GNinja \
-S api/rust/cmake-ffi/ \
-B cxx-bridge-build/ \
-DLIEF_DIR=$INSTALL_DIR/lib/cmake/LIEF \
-DLIEF_RUST_FFI_SRC=cxx-bridge/ \
-DCMAKE_INSTALL_PREFIX=cxx-bridge-out/
$ ninja -C cxx-bridge-build/