Cross compile rust-openssl for Raspberry Pi 2

后端 未结 2 1639
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-06 09:04

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

2条回答
  •  广开言路
    2021-02-06 09:47

    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
    

提交回复
热议问题