I am on a Debian machine and I want to cross compile a project for my Raspberry Pi 2. I\'ve managed to do it for a simple hello world using rustup, but couldn\'t figure out how
You must pass shared
option when configuring openssl compilation (this will make -fPIC
parameter be passed to the compiler).
Here is a sequence of commands that I used to test cross compiling a Rust program that prints the openssl version:
cd /tmp
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
tar xzf openssl-1.0.1t.tar.gz
export MACHINE=armv7
export ARCH=arm
export CC=arm-linux-gnueabihf-gcc
cd openssl-1.0.1t && ./config shared && make && cd -
export OPENSSL_LIB_DIR=/tmp/openssl-1.0.1t/
export OPENSSL_INCLUDE_DIR=/tmp/openssl-1.0.1t/include
cargo new xx --bin
cd xx
mkdir .cargo
cat > .cargo/config << EOF
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
EOF
cat > src/main.rs << EOF
extern crate openssl;
fn main() {
println!("{}", openssl::version::version())
}
EOF
cargo add openssl # requires cargo install cargo-add
cargo build --target armv7-unknown-linux-gnueabihf
Testing the compiled program on the host computer
Setting OPENSSL_STATIC
makes rust-openssl
be statically linked. If you use the static linked version of rust-openssl
, install a libc for armhf (crossbuild-essential-armhf
on Debian) and qemu-static
, you can the run the compiled program with the command:
qemu-arm-static target/armv7-unknown-linux-gnueabihf/debug/xx
This is an older question but it shows up highly on Google, so I wanted to call out that nowadays you don't need to manually compile OpenSSL (if you don't want to). The openssl
crate provides a vendored feature that causes OpenSSL to be compiled from source when you build your project.
You can propagate the feature into your own project to optionally depend on vendored
by adding something like this to your Cargo.toml
:
[features]
...
# If compiling on a system without OpenSSL installed, or cross-compiling for a different
# architecture, enable this feature to compile OpenSSL as part of the build.
# See https://docs.rs/openssl/#vendored for more.
static_ssl = ['openssl/vendored']
[dependencies]
...
[dependencies.openssl]
optional = true
version = ...
Enabling the static_ssl
feature when building your project will then compile OpenSSL against the same target architecture as the rest of your build.
This post goes into some more details about different ways of compiling with OpenSSL.