Rust Cross Compilation

I recently had a need to cross-compile a Rust binary for ARM64 architecture (Linux).

This little code snippet describes how I did it with Alpine linux base image.

# Register Qemu emulator for running aarch64 binaries in x86 (assumes docker running)
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes

# Run Alpine ARM64 image and enter docker 
docker run --rm -v /host/source/path:/src -it  arm64v8/alpine:3.16  /bin/sh


### ----IN DOCKER BEGIN----

# Install Rust
apk update
apk add curl openssl3-dev libgcc gcc musl-dev protobuf-dev openssl openssl3 sqlite-dev openssl-libs-static
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y

# go into src directory
cd /src


# Build package
/root/.cargo/bin/cargo build
### ----IN DOCKER END----

Cross-RS

https://github.com/cross-rs/cross?tab=readme-ov-file

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *