opencvrustcross-compilingapt

How to cross compile a rust project with opencv package for armv7


I have a simple rust application which compiles without errors on my Ubuntu host machine. The Cargo.toml looks like this:

[package]
name = "openvc-test"
version = "0.1.0"
edition = "2021"

[dependencies]
opencv = "0.66"

Then I tried to cross compile this project for armv7-unknown-linux-gnueabihf, so that I can run the binary on a raspberry. I used the cross tool for this purpose. The FAQ of the cross project has some information about how to handle external libraries. So I tried to install libopencv-dev:armhf on the docker image just like in the examples of the FAQ. However, I get the following error:

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

I also tried to use Debian repositories as described in the FAQ. The error is the same.

Does anyone have an idea on how to solve this issue? Or is there another way of cross compiling a rust project that I can try?

Compiling on the raspberry does not work, compilation gets stuck at package opencv. I think it is due to the limited performance of the rapsberry?


Solution

  • Installing a precompiled opencv package in the docker container does not work (still don't know why), but cross compiling the correct opencv version does work. See the following docker file:

    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"
    

    In Cross.toml you simply specify the path of the dockerfile, e.g.:

    [target.armv7-unknown-linux-gnueabihf]
    dockerfile = "./Dockerfile"
    

    Cross compiling the Rust project:

    cross build --target armv7-unknown-linux-gnueabihf
    

    The initial setup of the docker container takes quite a long time.