为 Raspberry Pi 2 交叉编译 rust-openssl
Cross compile rust-openssl for Raspberry Pi 2
我在一台 Debian 机器上,我想为我的 Raspberry Pi 2 交叉编译一个项目。我已经设法使用 rustup 为一个简单的 hello world 做到了,但不知道如何做交叉编译 rust-openssl crate。
我用 arm-linux-gnueabihf-gcc 编译了 openssl 并安装在我的 home/opensslArm
目录中。
当我运行
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.
如果我导出有问题的变量,我会得到同样的错误。
我不知道我到底应该做什么,我不是交叉编译方面的专家。有没有人设法做到这一点?
编辑:我使用的是 rust-openssl 0.7.11。升级到 0.7.13 解决了这个问题(我现在可以看到 cargo 编译 rust-openssl 依赖没有错误)但我现在有另一个:
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 吗?
配置openssl编译时必须传递shared
选项(这将使-fPIC
参数传递给编译器)。
这是我用来测试交叉编译打印 openssl 版本的 Rust 程序的一系列命令:
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
在上位机测试编译好的程序
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) 和qemu-static
, 你可以运行 编译程序用命令:
qemu-arm-static target/armv7-unknown-linux-gnueabihf/debug/xx
这是一个较旧的问题,但它在 Google 上的出现率很高,所以我想指出,现在您不需要手动编译 OpenSSL(如果您不想的话)。 openssl
crate 提供了一个 vendored
feature 可以在您构建项目时从源代码编译 OpenSSL。
您可以将此功能传播到您自己的项目中,以选择性地依赖 vendored
,方法是将类似的内容添加到您的 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 = ...
在构建项目时启用 static_ssl
功能将针对与构建其余部分相同的目标体系结构编译 OpenSSL。
This post 详细介绍了使用 OpenSSL 进行编译的不同方式。
我在一台 Debian 机器上,我想为我的 Raspberry Pi 2 交叉编译一个项目。我已经设法使用 rustup 为一个简单的 hello world 做到了,但不知道如何做交叉编译 rust-openssl crate。
我用 arm-linux-gnueabihf-gcc 编译了 openssl 并安装在我的 home/opensslArm
目录中。
当我运行
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.
如果我导出有问题的变量,我会得到同样的错误。
我不知道我到底应该做什么,我不是交叉编译方面的专家。有没有人设法做到这一点?
编辑:我使用的是 rust-openssl 0.7.11。升级到 0.7.13 解决了这个问题(我现在可以看到 cargo 编译 rust-openssl 依赖没有错误)但我现在有另一个:
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 吗?
配置openssl编译时必须传递shared
选项(这将使-fPIC
参数传递给编译器)。
这是我用来测试交叉编译打印 openssl 版本的 Rust 程序的一系列命令:
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
在上位机测试编译好的程序
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) 和qemu-static
, 你可以运行 编译程序用命令:
qemu-arm-static target/armv7-unknown-linux-gnueabihf/debug/xx
这是一个较旧的问题,但它在 Google 上的出现率很高,所以我想指出,现在您不需要手动编译 OpenSSL(如果您不想的话)。 openssl
crate 提供了一个 vendored
feature 可以在您构建项目时从源代码编译 OpenSSL。
您可以将此功能传播到您自己的项目中,以选择性地依赖 vendored
,方法是将类似的内容添加到您的 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 = ...
在构建项目时启用 static_ssl
功能将针对与构建其余部分相同的目标体系结构编译 OpenSSL。
This post 详细介绍了使用 OpenSSL 进行编译的不同方式。