为ARM交叉编译opencv项目并在go项目gocv中调用
Cross compiling opencv project for ARM and called in go project gocv
我在主机Ubuntu20.04.3 LTS下编译了opencv和gocv for ARM,步骤如下
1.安装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
我的问题是如何 运行 android 中的 ELF version
使用 arm 处理器?
我将 install/libs
中的静态库复制到手机 /system/lib/
中。将 version
文件复制到 /data/local/tmp
并授予执行权限。
然后执行 version
得到输出“./version:没有那个文件或目录”。
130|cactus:/data/local/tmp # ./version
/system/bin/sh: ./version: No such file or directory
我发现原因是动态 link 解释器 android 没有 linux 平台的解释器。
用 NDK 重建 opencv 后得到静态库。然后用NDK的clang编译器编译go项目,问题就解决了
我在主机Ubuntu20.04.3 LTS下编译了opencv和gocv for ARM,步骤如下
1.安装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
我的问题是如何 运行 android 中的 ELF version
使用 arm 处理器?
我将 install/libs
中的静态库复制到手机 /system/lib/
中。将 version
文件复制到 /data/local/tmp
并授予执行权限。
然后执行 version
得到输出“./version:没有那个文件或目录”。
130|cactus:/data/local/tmp # ./version
/system/bin/sh: ./version: No such file or directory
我发现原因是动态 link 解释器 android 没有 linux 平台的解释器。 用 NDK 重建 opencv 后得到静态库。然后用NDK的clang编译器编译go项目,问题就解决了