Compiling Rust for RISC-V

May 25, 2025

Today, I worked on a small example on how to compile a Rust program targeting a RISC-V architecture. Essentially, you add the correct target

rustup target add riscv64gc-unknown-linux-gnu

then configure the linker to use the appropriate GNU GCC linker and also the runner to QEMU, and statically link the C libraries.

[target.riscv64gc-unknown-linux-gnu]
linker = "riscv64-linux-gnu-gcc"
rustflags = ["-C", "target-feature=+crt-static"]
runner = "qemu-riscv64"

You can run the program with

cargo run --target riscv64gc-unknown-linux-gnu

#rust #riscv #cross-compilation