问题描述
我在Debian机器上,并且想为Raspberry Pi 2交叉编译一个项目.我已经成功地使用rustup在一个简单的hello世界中完成了该项目,但无法弄清楚如何交叉编译rust -openssl板条箱.
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 to cross compile the rust-openssl crate.
我已经用arm-linux-gnueabihf-gcc编译了openssl并将其安装在我的home/opensslArm
目录中.
I have compiled openssl with arm-linux-gnueabihf-gcc and installed it in my home/opensslArm
directory.
我跑步时
OPENSSL_LIB_DIR=/home/johann/opensslArm/lib OPENSSL_INCLUDE_DIR=/home/johann/opensslArm/include cargo build --target=armv7-unknown-linux-gnueabihf
我收到此错误:
failed to run custom build command for `openssl-sys-extras v0.7.11`
Process didn't exit successfully: `/home/johann/projects/test/target/debug/build/openssl-sys-extras-e1c84960cd35bc93/build-script-build` (exit code: 101)
--- stdout
TARGET = Some("armv7-unknown-linux-gnueabihf")
OPT_LEVEL = Some("0")
PROFILE = Some("debug")
TARGET = Some("armv7-unknown-linux-gnueabihf")
debug=true opt-level=0
HOST = Some("x86_64-unknown-linux-gnu")
TARGET = Some("armv7-unknown-linux-gnueabihf")
TARGET = Some("armv7-unknown-linux-gnueabihf")
HOST = Some("x86_64-unknown-linux-gnu")
CC_armv7-unknown-linux-gnueabihf = None
CC_armv7_unknown_linux_gnueabihf = None
TARGET_CC = None
CC = None
HOST = Some("x86_64-unknown-linux-gnu")
TARGET = Some("armv7-unknown-linux-gnueabihf")
HOST = Some("x86_64-unknown-linux-gnu")
CFLAGS_armv7-unknown-linux-gnueabihf = None
CFLAGS_armv7_unknown_linux_gnueabihf = None
TARGET_CFLAGS = None
CFLAGS = None
running: "arm-linux-gnueabihf-gcc" "-O0" "-ffunction-sections" "-fdata-sections" "-g" "-fPIC" "-march=armv7-a" "-o" "/home/johann/projects/test/target/armv7-unknown-linux-gnueabihf/debug/build/openssl-sys-extras-e1c84960cd35bc93/out/src/openssl_shim.o" "-c" "src/openssl_shim.c"
ExitStatus(ExitStatus(256))
command did not execute successfully, got: exit code: 1
--- stderr
In file included from src/openssl_shim.c:1:0:
/usr/include/openssl/hmac.h:61:34: fatal error: openssl/opensslconf.h: No such file or directory
compilation terminated.
thread '<main>' panicked at 'explicit panic', /home/johann/.cargo/registry/src/github.com-88ac128001ac3a9a/gcc-0.3.28/src/lib.rs:840
note: Run with `RUST_BACKTRACE=1` for a backtrace.
如果导出有问题的变量,则会出现相同的错误.
I get the same error if I export the variables in question.
我不知道该怎么做,我不是交叉编译专家.有没有人设法做到这一点?
I don't know exactly what I am supposed to do, I am not an expert in cross compiling. Has anyone managed to do this?
我正在使用rust-openssl 0.7.11.升级到0.7.13可以解决此问题(我现在可以看到无误地编译了rust-openssl依赖的货物),但是现在我有了另一个问题:
I was using rust-openssl 0.7.11. Upgrading to 0.7.13 fixed this issue (I can now see cargo compiling rust-openssl dependencies without an error) but I have now another one:
error: linking with `arm-linux-gnueabihf-gcc` failed: exit code: 1
...
note: /usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/bin/ld: /home/johann/opensslArm/lib/libssl.a(s23_meth.o): relocation R_ARM_THM_MOVW_ABS_NC against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/home/johann/opensslArm/lib/libssl.a: error adding symbols: Bad value
如何添加-fPIC
标志?我应该重新编译带有特定标志的opensslArm吗?
How can I add -fPIC
flag? Should I recompile opensslArm with specific flags?
推荐答案
在配置openssl编译时,您必须传递shared
选项(这会将-fPIC
参数传递给编译器).
You must pass shared
option when configuring openssl compilation (this will make -fPIC
parameter be passed to the compiler).
以下是我用来测试交叉编译显示opensl版本的Rust程序的命令序列:
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
在主机上测试编译的程序
设置 OPENSSL_STATIC
使rust-openssl
静态链接.如果您使用rust-openssl
的静态链接版本,请为armhf安装一个libc(crossbuild-essential-armhf
在Debian上 )和qemu-static
,您可以使用以下命令运行已编译的程序:
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
这篇关于交叉编译Raspberry Pi 2的rust-openssl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!