为 ARM 交叉编译 opencv 项目并在 go 项目 gocv 中调用

Posted

技术标签:

【中文标题】为 ARM 交叉编译 opencv 项目并在 go 项目 gocv 中调用【英文标题】:Cross compiling opencv project for ARM and called in go project gocv 【发布时间】:2022-01-21 07:44:58 【问题描述】:

我在主机Ubuntu 20.04.3 LTS下为ARM编译opencv和gocv,步骤如下。

1. apt install arm-linux-gnueabi-gcc, arm-linux-gnueabi-g++

2。编译安装opencv

cmake -DCMAKE_C_COMPILER=arm-linux-gnueabi-gcc -DCMAKE_CXX_COMPILER=arm-linux-gnueabi-g++  -DCMAKE_TOOLCHAIN_FILE=../opencv/platforms/linux/arm-gnueabi.toolchain.cmake  ../opencv
make -j16
make install

安装后。在当前目录中创建了一个名为install 的文件夹,并获得了如下共享库。

../arm2build/install/lib
|-- cmake
|   `-- opencv4
|       |-- OpenCVConfig-version.cmake
|       |-- OpenCVConfig.cmake
|       |-- OpenCVModules-release.cmake
|       `-- OpenCVModules.cmake
|-- libopencv_calib3d.so -> libopencv_calib3d.so.405
|-- libopencv_calib3d.so.4.5.4
|-- libopencv_calib3d.so.405 -> libopencv_calib3d.so.4.5.4
|-- libopencv_core.so -> libopencv_core.so.405
|-- libopencv_core.so.4.5.4
|-- libopencv_core.so.405 -> libopencv_core.so.4.5.4
|-- libopencv_dnn.so -> libopencv_dnn.so.405
|-- libopencv_dnn.so.4.5.4
|-- libopencv_dnn.so.405 -> libopencv_dnn.so.4.5.4
|-- libopencv_features2d.so -> libopencv_features2d.so.405
|-- libopencv_features2d.so.4.5.4
|-- libopencv_features2d.so.405 -> libopencv_features2d.so.4.5.4
|-- libopencv_flann.so -> libopencv_flann.so.405
|-- libopencv_flann.so.4.5.4
|-- libopencv_flann.so.405 -> libopencv_flann.so.4.5.4
|-- libopencv_gapi.so -> libopencv_gapi.so.405
|-- libopencv_gapi.so.4.5.4
|-- libopencv_gapi.so.405 -> libopencv_gapi.so.4.5.4
|-- libopencv_highgui.so -> libopencv_highgui.so.405
|-- libopencv_highgui.so.4.5.4
|-- libopencv_highgui.so.405 -> libopencv_highgui.so.4.5.4
|-- libopencv_imgcodecs.so -> libopencv_imgcodecs.so.405
|-- libopencv_imgcodecs.so.4.5.4
|-- libopencv_imgcodecs.so.405 -> libopencv_imgcodecs.so.4.5.4
|-- libopencv_imgproc.so -> libopencv_imgproc.so.405
|-- libopencv_imgproc.so.4.5.4
|-- libopencv_imgproc.so.405 -> libopencv_imgproc.so.4.5.4
|-- libopencv_ml.so -> libopencv_ml.so.405
|-- libopencv_ml.so.4.5.4
|-- libopencv_ml.so.405 -> libopencv_ml.so.4.5.4
|-- libopencv_objdetect.so -> libopencv_objdetect.so.405
|-- libopencv_objdetect.so.4.5.4
|-- libopencv_objdetect.so.405 -> libopencv_objdetect.so.4.5.4
|-- libopencv_photo.so -> libopencv_photo.so.405
|-- libopencv_photo.so.4.5.4
|-- libopencv_photo.so.405 -> libopencv_photo.so.4.5.4
|-- libopencv_stitching.so -> libopencv_stitching.so.405
|-- libopencv_stitching.so.4.5.4
|-- libopencv_stitching.so.405 -> libopencv_stitching.so.4.5.4
|-- libopencv_video.so -> libopencv_video.so.405
|-- libopencv_video.so.4.5.4
|-- libopencv_video.so.405 -> libopencv_video.so.4.5.4
|-- libopencv_videoio.so -> libopencv_videoio.so.405
|-- libopencv_videoio.so.4.5.4
`-- libopencv_videoio.so.405 -> libopencv_videoio.so.4.5.4

3.使用 cgo 构建 gocv/cmd/version 项目

CGO_ENABLED=1 CC=arm-linux-gnueabi-gcc CXX=arm-linux-gnueabi-g++ CGO_CPPFLAGS='-I/opt/arm2build/install/include/opencv4' CGO_LDFLAGS='-L/opt/arm2build/lib -lopencv_core -lopencv_videoio -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs -lopencv_objdetect -lopencv_features2d -lopencv_video -lopencv_dnn -lopencv_calib3d -lopencv_flann -lopencv_photo' GOOS=linux GOARCH=arm go build -tags=customenv .

之后会生成ELF

root@793d7120f9df:/opt/go/gocv/cmd/version# file version
version: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, BuildID[sha1]=038c80350b7a0c9e72b10021c66c31c2dbb4df2c, for GNU/Linux 3.2.0, not stripped

我的问题是如何在带有 arm 处理器的 android 中运行 ELF version

我将install/libs 中的静态库复制到手机的/system/lib/。将version 文件复制到/data/local/tmp 并授予执行权限。 然后执行version得到输出“./version: No such file or directory”。

130|cactus:/data/local/tmp # ./version
/system/bin/sh: ./version: No such file or directory

【问题讨论】:

我发现问题是 cgo 使用不存在动态链接解释器。 【参考方案1】:

我发现原因是android没有linux平台解释器的动态链接解释器。 用 NDK 重建 opencv 后得到静态库。并用NDK的clang编译器构建go项目,问题就解决了。

【讨论】:

以上是关于为 ARM 交叉编译 opencv 项目并在 go 项目 gocv 中调用的主要内容,如果未能解决你的问题,请参考以下文章

移植opencv3.4到Nanopi NEO 交叉编译

经验分享华为海思Hi35xx系列ARM32交叉编译opencv

经验分享华为atlas500系列aarch64交叉编译opencv

opencv arm交叉编译与仿真验证详细流程

为 arm 交叉编译 iptables 的问题

arm-linux交叉编译器的路径设置问题,怎么办????