Linux交叉编译opencv并移植ARM端 - 知乎
一、安装交叉编译器
目标平台为arm7l,此为32位ARM架构,要安装合适的编译器
sudo apt install arm-linux-gnueabihf-gcc
sudo apt install arm-linux-gnueabihf-g++
注意:64位ARM架构的编译器与32位ARM架构的编译器不能通用
二、opencv交叉编译
opencv的交叉编译工具链在../opencv3.2.0/platforms/linux 路径下,linux文件夹下是一些.cmake文件,对应不同的移植对象,我需要在rv1126上部署模型,所以选择platform/linux/arm-gnueabi.toolchain.cmake
你也可以对其作出修改(添加编译器所在位置):
set(GCC_COMPILER_VERSION "" CACHE STRING "GCC Compiler version")
set(GNU_MACHINE "arm-linux-gnueabi" CACHE STRING "GNU compiler triple")set(CMAKE_C_COMPILER /usr/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabihf-g++)include("${CMAKE_CURRENT_LIST_DIR}/arm.toolchain.cmake")
接下来执行交叉编译命令
mkdir build && cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/mnt/usr/local -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv/opencv_contrib/modules/ -D OPENCV_ENABLE_NONFREE=True -DCMAKE_TOOLCHAIN_FILE=/opt/opencv/platforms/linux/arm-gnueabi.toolchain.cmake ..sudo make -j8
sudo make install
编译结束后进入 build 文件夹,里面生成bin,include,lib,share四个文件夹
三、Linux上生成ARM架构下可执行文件
创建测试用例test.cpp,内容如下
#include <opencv2/opencv.hpp>using namespace cv; int main( int argc, char** argv )
{ Mat image; image = imread( "7.bmp", 1 ); if( !image.data ) { printf( "No image data \n" ); return -1; } cv::Point lu = cv::Point(180, 60); cv::Point rd = cv::Point(400, 260); cv::rectangle(image, lu, rd, cv::Scalar( 255, 20, 0 ), 1, cv::LINE_AA ); imwrite("8.bmp", image); return 0;
}
CMakeList.txt
cmake_minimum_required(VERSION 2.8.4)project(draw_image)
include ($ENV{HOME}/configs/cross.cmake)find_package(OpenCV REQUIRED)
add_executable(${CMAKE_PROJECT_NAME} test.cpp)
target_include_directories(draw_image PUBLIC ${OpenCV_INCLUDE_DIRS})
target_link_libraries(draw_image PRIVATE ${OpenCV_LIBS})
~
在终端执行:
mkdir build && cd buid
cmake ..
make
在test.cpp同一目录下会生成可执行文件 draw_image,利用file draw_image检查可执行文件格式是否支持arm
draw_image: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=6030f6191b409626a1f54c996cebb91aad644061, for GNU/Linux 3.2.0, not stripped
出现如上结果说明可执行文件支持ARM7l
四、移植ARM端执行
将 build文件夹里面的include和lib里的文件分别拷贝到ARM板的/usr/include 和/usr/lib下 然后打开终端执行可执行文件即可。
build文件里include也许是空的,你可以在安装目录/include/找到opencv的头文件
draw_image可以执行