我有一个简单的 Rust 应用程序,它在我的 Ubuntu 主机上编译时没有错误。 Cargo.toml 看起来像这样:
[package]
name = "openvc-test"
version = "0.1.0"
edition = "2021"
[dependencies]
opencv = "0.66"
然后我尝试为armv7-unknown-linux-gnueabihf交叉编译这个项目,以便我可以在树莓派上运行二进制文件。为此,我使用了 cross 工具。跨项目的FAQ有一些关于如何处理外部库的信息。所以我尝试在 docker 镜像上安装
libopencv-dev:armhf
,就像常见问题解答的示例一样。但是,我收到以下错误:
The following packages have unmet dependencies:
libopencv-dev:armhf : Depends: libopencv3.2-java:armhf (= 3.2.0+dfsg-6) but it is not installable
Recommends: opencv-data:armhf but it is not installable
我还尝试使用常见问题解答中所述的 Debian 存储库。错误是一样的。
有人知道如何解决这个问题吗?或者我可以尝试另一种交叉编译 Rust 项目的方法吗?
在树莓派上编译不行,编译卡在opencv包上。我想是因为Raspberry性能有限?
在docker容器中安装预编译的opencv包不起作用(仍然不知道为什么),但是交叉编译正确的opencv版本确实起作用。请参阅以下 docker 文件:
ARG CROSS_BASE_IMAGE
FROM $CROSS_BASE_IMAGE
# requirements of bindgen, see https://rust-lang.github.io/rust-bindgen/requirements.html
RUN DEBIAN_FRONTEND=noninteractive apt install -y llvm-dev libclang-dev clang
# cross compile opencv, see https://docs.opencv.org/4.x/d0/d76/tutorial_arm_crosscompile_with_cmake.html
RUN DEBIAN_FRONTEND=noninteractive apt install -y gcc-arm-linux-gnueabihf git build-essential cmake
RUN git clone --depth 1 --branch '4.5.1' https://github.com/opencv/opencv.git && \
cd opencv/platforms/linux && \
mkdir build && \
cd build && \
cmake -DCMAKE_TOOLCHAIN_FILE=../arm-gnueabi.toolchain.cmake ../../.. && \
make && \
make install
ENV CMAKE_PREFIX_PATH="/opencv/platforms/linux/build/install"
在
Cross.toml
中,您只需指定dockerfile的路径,例如:
[target.armv7-unknown-linux-gnueabihf]
dockerfile = "./Dockerfile"
交叉编译 Rust 项目:
cross build --target armv7-unknown-linux-gnueabihf
docker容器的初始设置需要相当长的时间。
我发现板条箱的 github 页面有一个可用的 docker 镜像: https://github.com/twistedfall/opencv-rust/tree/master#crosscompilation
我还没有让它正常工作
cross
,但如果你将你的存储库挂载到该映像中并使用cargo-xbuild
构建,它就可以正常工作:
# follow instructions in repo to get rpi-xcompile image
docker run --rm -v .:/src -it rpi-xcompile /bin/bash
cd /src
cargo-xbuild