aarch64-linux-g++ 6.40交叉编译环境的特殊性是:用网上一般的Opencv编译方法可以成功编并安装,但是不能编译出应用程序!编译C/C++ 程序时会提示:
致命错误 <opencv2/opencv.hpp> 无这个文件或目录!
检查pkg-config,opencv.pc,bash.bashrc 等设置都不能解决问题。后来发现是Cmake-gui设置问题,正确方法如下:
一、Opencv 安装
root@li-desktop:/home/li/usr/opencv-3.4.2# cmake-gui
1.cmake 设置
打开cmake,设置Opencv文件夹和MAKE文件夹,然后点Configure下一步设置。
在弹出的窗口中选择第三个 Specify toolchain File crosscompiling ,这是Opencv官方针对aarch64-linux-gcc 优化的toolchain,所以一定能顺利通过cmake。
Next ,这一步选取官方Make方案文件,进入opencv3.4.2源文件夹里的platforms文件夹,选取 aarch64-gnu.toolchain.cmake文件。
点open后完成设置。选取你的安装目标路径CMAKE_INSTALL_PREFIX,选择自己需要的包进行安装,然后点Configrue,完成后点Generate完成Cmake 。
2. make
root@li-desktop:/home/li/usr/opencv-3.4.2/opencv3arm/# make -j4 //14核
[100%] Built target opencv_test_calib3d
[100%] Linking CXX executable ../../bin/opencv_perf_stitching
[100%] Built target opencv_perf_stitching
3. make install
root@li-desktop:/home/li/usr/opencv-3.4.2/opencv342arm# make install
[ 0%] Built target gen-pkgconfig
[ 2%] Built target zlib
[ 6%] Built target libjpeg-turbo
[ 9%] Built target libtiff
[ 18%] Built target libwebp
。。。。
-- Installing: /usr/local/arm/opencv342arm/bin/opencv_annotation
-- Installing: /usr/local/arm/opencv342arm/bin/opencv_visualisation
-- Installing: /usr/local/arm/opencv342arm/bin/opencv_interactive-calibration
-- Installing: /usr/local/arm/opencv342arm/bin/opencv_version
root@li-desktop:/home/li/usr/opencv-3.4.2/opencv342arm#
//安装完成!
二、Opencv工作环境设置
1. 设置pkg-config路径
vim /etc/bash.bashrc
在bashrc最后行加入以下路径,具体跟据自己的安装路径更改。
export PKG_CONFIG_LIBDIR=/usr/local/arm/opencv342arm/lib/pkgconfig
xport PKG_CONFIG_PATH
//按i编辑,安Esc完成,按Shift+zz 保存退出。必须在root下才能保存。
//完成后刷新bash.bashrc
source /etc/bash.bashrc
2.opencv.pc
以上路径是告诉pkg-config到/usr/local/arm/opencv342/lib/pkgconfig目录下寻找opencv.pc文件。opencv.pc才是重点!里面记录了incudle和lib 库文件和头文件的路径,具体见下:
# Package Information for pkg-config
prefix=/usr/local/arm/opencv342arm //安装路径
exec_prefix=${prefix}
libdir=${exec_prefix}/lib //库文件路径
includedir_old=${prefix}/include/opencv //头文件路径
includedir_new=${prefix}/include //头文件路径
Name: OpenCV //名称
Description: Open Source Computer Vision Library
Version: 3.4.2
Libs: -L${exec_prefix}/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}
Libs:是安装的库文件。名称可以自己改。
3.测试
(1).测试安装版本,其实是测试opencv.pc路径
pkg-config --modversion opencv
3.4.2
//如有多个opencv版本,只要把安装目录与opencvXX.pc取相对应的名称
//并把不同的XXX.pc文件放在同一个pkgconfig目录下则可
(2).测试库文件与头文件
$pkg-config --cflags --libs opencv
-I/usr/local/arm/opencv342arm/include/opencv -I/usr/local/arm/opencv342arm/include -L/usr/local/arm/opencv342arm/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
三、编译程序
用摄像头读取程序测试,名称为camtest2.cpp,把它放到 /home 目录。
//camtest2.cpp
//摄像头读取程序
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc,char **argv)
{
VideoCapture capture(0);
namedWindow("hhh",CV_WINDOW_AUTOSIZE);
while(true)
{
Mat frame;
capture>>frame;
imshow("usb cam",frame);
waitKey(30);
}
return 0;
}
进入/home 打开终端输入:
$cd /home
$aarch64-linux-g++ `pkg-config --cflags opencv` camtest2.cpp -o camtest2 `pkg-config --libs opencv`
如果没错误提示,在/home目录下会生成camtest2二进制可执行文件,当然这个文件只能在arm板上执行。
四、移植
移植是指把opencv库移植到ARM开发板上,把Opencv安装目录下lib目录里的所有库文件复制到开发板/usr/lib下就行了。
完。