将OpenCV项目交叉编译为ARM架构,并在Go项目gocv中调用。

huangapple go评论81阅读模式
英文:

Cross compiling opencv project for ARM and called in go project gocv

问题

我在主机Ubuntu 20.04.3 LTS上为ARM编译了OpenCV和GoCV,按照以下步骤进行操作:

1. 安装arm-linux-gnueabi-gcc和arm-linux-gnueabi-g++

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

我将静态库复制到手机的/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
英文:

I compiled opencv and gocv for ARM under the host Ubuntu 20.04.3 LTS follows the steps as below.

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

2. Compile and install 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

After installed. A folder named install was created in current dir and got shared libs as below.

../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. Build gocv/cmd/version project with cgo

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 .

After do that will generate 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

My questions is how to run the ELF version in android with arm processor?

I copied the static libraries in install/libs to mobile's /system/lib/. copied the version file to /data/local/tmp and grant execute privilege.
Then exec the version got output "./version: No such file or directory".

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

答案1

得分: 0

我发现问题的原因是动态链接解释器,Android平台上没有Linux平台的解释器。重新使用NDK构建OpenCV并获取静态库,然后使用NDK的Clang编译器构建Go项目,问题将得到解决。

英文:

I found the reason is about dynamic link interpreter which android do not has linux platform's interpreter.
After rebuild opencv with NDK got static libs. And build go project with NDK's clang compiler, the problem would be solved.

huangapple
  • 本文由 发表于 2021年12月19日 13:25:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/70409155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定