1.下载源码

https://github.com/opencv/opencv.git

2.安装cmake-gui工具

sudo apt-get install cmake-qt-gui
3.进入opencv源码,新建build文件夹,并进入

4.配置安装选项,进入cmake-gui

进入后界面

选择交叉编译工具 

 

配置交叉编译工具路径

 

 输入Z,选中zip库

 输入install 进行安装目录配置

输入QT,进行QT环境配置

点击Configure 点击Generate,直到显示Configuring done Generating done

5.make -j8

6.sudo make insatll 

7.编译过程ERROR

没有设置CMAKE_EXE_LINKER_FLAGS

修改build/CMakeCache.txt的文件
CMAKE_EXE_LINKER_FLAGS:STRING=-pthread -ldl -lrt
 

../../lib/libopencv_core.so: undefined reference to `dlclose'
../../lib/libopencv_core.so: undefined reference to `dlsym'
../../lib/libopencv_core.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
Scanning dependencies of target opencv_imgcodecs
apps/version/CMakeFiles/opencv_version.dir/build.make:95: recipe for target 'bin/opencv_version' failed
make[2]: *** [bin/opencv_version] Error 1
CMakeFiles/Makefile2:4018: recipe for target 'apps/version/CMakeFiles/opencv_version.dir/all' failed
make[1]: *** [apps/version/CMakeFiles/opencv_version.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 34%] Processing OpenCL kernels (photo)
[ 34%] Processing OpenCL kernels (features2d)
[ 34%] Processing OpenCL kernels (dnn)
Scanning dependencies of target opencv_photo
Scanning dependencies of target opencv_features2d
Scanning dependencies of target opencv_dnn
 

gapi模块报错,暂时未解决,

[ 90%] Building CXX object modules/gapi/CMakeFiles/opencv_gapi.dir/src/backends/ocl/goclimgproc.cpp.obj
[ 90%] Building CXX object modules/gapi/CMakeFiles/opencv_gapi.dir/src/backends/ocl/goclcore.cpp.obj
[ 90%] Building CXX object modules/gapi/CMakeFiles/opencv_gapi.dir/src/backends/ie/giebackend.cpp.obj
In file included from /home/ubuntu/Downloads/opencv/modules/gapi/src/backends/ie/giebackend.hpp:11:0,
                 from /home/ubuntu/Downloads/opencv/modules/gapi/src/backends/ie/giebackend.cpp:11:
/home/ubuntu/Downloads/opencv/modules/gapi/include/opencv2/gapi/infer/ie.hpp: In constructor 'cv::gapi::ie::Params<cv::gapi::Generic>::Params(const string&, const string&, const string&, const string&)':
/home/ubuntu/Downloads/opencv/modules/gapi/include/opencv2/gapi/infer/ie.hpp:374:20: error: converting to 'std::unordered_map<std::basic_string<char>, std::pair<cv::Mat, cv::gapi::ie::TraitAs> >' from initializer list would use explicit constructor 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with _Key = std::basic_string<char>; _Tp = std::pair<cv::Mat, cv::gapi::ie::TraitAs>; _Hash = std::hash<std::basic_string<char> >; _Pred = std::equal_to<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::pair<cv::Mat, cv::gapi::ie::TraitAs> > >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type = long unsigned int; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hasher = std::hash<std::basic_string<char> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_equal = std::equal_to<std::basic_string<char> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::allocator_type = std::allocator<std::pair<const std::basic_string<char>, std::pair<cv::Mat, cv::gapi::ie::TraitAs> > >]'
           m_tag(tag) {
                    ^
/home/ubuntu/Downloads/opencv/modules/gapi/include/opencv2/gapi/infer/ie.hpp:374:20: error: converting to 'std::unordered_set<std::basic_string<char> >' from initializer list would use explicit constructor 'std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set(std::unordered_set<_Value, _Hash, _Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with _Value = std::basic_string<char>; _Hash = std::hash<std::basic_string<char> >; _Pred = std::equal_to<std::basic_string<char> >; _Alloc = std::allocator<std::basic_string<char> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::size_type = long unsigned int; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::hasher = std::hash<std::basic_string<char> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::key_equal = std::equal_to<std::basic_string<char> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]'
/home/ubuntu/Downloads/opencv/modules/gapi/include/opencv2/gapi/infer/ie.hpp: In constructor 'cv::gapi::ie::Params<cv::gapi::Generic>::Params(const string&, const string&, const string&)':
/home/ubuntu/Downloads/opencv/modules/gapi/include/opencv2/gapi/infer/ie.hpp:392:20: error: converting to 'std::unordered_map<std::basic_string<char>, std::pair<cv::Mat, cv::gapi::ie::TraitAs> >' from initializer list would use explicit constructor 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with _Key = std::basic_string<char>; _Tp = std::pair<cv::Mat, cv::gapi::ie::TraitAs>; _Hash = std::hash<std::basic_string<char> >; _Pred = std::equal_to<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::pair<cv::Mat, cv::gapi::ie::TraitAs> > >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type = long unsigned int; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hasher = std::hash<std::basic_string<char> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_equal = std::equal_to<std::basic_string<char> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::allocator_type = std::allocator<std::pair<const std::basic_string<char>, std::pair<cv::Mat, cv::gapi::ie::TraitAs> > >]'
           m_tag(tag) {
                    ^
/home/ubuntu/Downloads/opencv/modules/gapi/include/opencv2/gapi/infer/ie.hpp:392:20: error: converting to 'std::unordered_set<std::basic_string<char> >' from initializer list would use explicit constructor 'std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set(std::unordered_set<_Value, _Hash, _Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with _Value = std::basic_string<char>; _Hash = std::hash<std::basic_string<char> >; _Pred = std::equal_to<std::basic_string<char> >; _Alloc = std::allocator<std::basic_string<char> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::size_type = long unsigned int; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::hasher = std::hash<std::basic_string<char> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::key_equal = std::equal_to<std::basic_string<char> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]'
modules/gapi/CMakeFiles/opencv_gapi.dir/build.make:1574: recipe for target 'modules/gapi/CMakeFiles/opencv_gapi.dir/src/backends/ie/giebackend.cpp.obj' failed
make[2]: *** [modules/gapi/CMakeFiles/opencv_gapi.dir/src/backends/ie/giebackend.cpp.obj] Error 1
make[2]: *** Waiting for unfinished jobs....
CMakeFiles/Makefile2:3400: recipe for target 'modules/gapi/CMakeFiles/opencv_gapi.dir/all' failed
make[1]: *** [modules/gapi/CMakeFiles/opencv_gapi.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
不包含此模块

重新configure generate,之后make

8.出现错误

把编译好的库文件和头文件复制到交叉编译工具的include 和lib 文件夹下

ubuntu@ubuntu:~/opencv_loongson$ mips64el-linux-g++   -std=c++11 -I/opt/gcc-4.9.3-64-gnu/include -L/opt/gcc-4.9.3-64-gnu/lib   -lopencv_core -lopencv_highgui -lopencv_imgcodecs  -lpthread -lrt -o  test test.cpp
/opt/gcc-4.9.3-64-gnu/lib/libopencv_core.so: undefined reference to `dladdr'
/opt/gcc-4.9.3-64-gnu/lib/libopencv_core.so: undefined reference to `dlopen'
/opt/gcc-4.9.3-64-gnu/lib/libopencv_core.so: undefined reference to `dlclose'
/opt/gcc-4.9.3-64-gnu/lib/libopencv_core.so: undefined reference to `dlsym'
 

编译选项添加 -ldl

ubuntu@ubuntu:~/opencv_loongson$ mips64el-linux-g++   -std=c++11 -I/opt/gcc-4.9.3-64-gnu/include -L/opt/gcc-4.9.3-64-gnu/lib   -lopencv_core -lopencv_highgui -lopencv_imgcodecs -ldl  -lpthread  -o  test test.cpp

编译成功

9.移植开发板

移植交叉编译好的库文件到开发板/lib目录下

运行程序,出现错误

# ./test
start add picture......

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.5.4-dev) /home/ubuntu/Downloads/opencv/modules/highgui/src/window.cpp:1334: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvWaitKey'

Aborted
# ./test
start add picture......

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.5.4-dev) /home/ubuntu/Downloads/opencv/modules/highgui/src/window.cpp:1334: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvWaitKey'

Aborted
# gui/src/window.cpp:1334: error: (-2:Unspecified error) The function is not imp
lemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you ar
e on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake o
r configure script in function 'cvWaitKey'
-sh: syntax error: unexpected "("
#
 

Ubuntu安装sudo apt install libgtk2.0-dev pkg-config,重新编译

配置QT5,选填交叉编译好的路径,如图

重走上面的流程

出现错误

CMakeFiles/Makefile2:1564: recipe for target 'modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/all' failed
make[1]: *** [modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/all] Error 2
/opt/gcc-4.9.3-64-gnu/lib/gcc/mips64el-linux/4.9.3/../../../../mips64el-linux/bin/ld: warning: libQt5Widgets.so.5, needed by ../../lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
/opt/gcc-4.9.3-64-gnu/lib/gcc/mips64el-linux/4.9.3/../../../../mips64el-linux/bin/ld: warning: libQt5Test.so.5, needed by ../../lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
/opt/gcc-4.9.3-64-gnu/lib/gcc/mips64el-linux/4.9.3/../../../../mips64el-linux/bin/ld: warning: libQt5Concurrent.so.5, needed by ../../lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
/opt/gcc-4.9.3-64-gnu/lib/gcc/mips64el-linux/4.9.3/../../../../mips64el-linux/bin/ld: warning: libQt5Gui.so.5, needed by ../../lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
/opt/gcc-4.9.3-64-gnu/lib/gcc/mips64el-linux/4.9.3/../../../../mips64el-linux/bin/ld: warning: libQt5Core.so.5, needed by ../../lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
../../lib/libopencv_highgui.so: undefined reference to `QPushButton::paintEvent(QPaintEvent*)@Qt_5'
../../lib/libopencv_highgui.so: undefined reference to `QWidget::setFocus(Qt::FocusReason)@Qt_5'
 

3个步骤,最后没确定到底是那个生效了哪个

1.拷贝编译好的QT库到交叉编译工具的lib目录下

2.拷贝编译好的QT库到opencv/build下的lib目录下

3.修改环境变量~/.bashrc,最后添加一句

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/qtMips/lib:/opt/gcc-4.9.3-64-gnu/lib
 

make clean ,之后make -j8

笔者版本

ubuntu@ubuntu:~/Downloads/opencv$ git branch
* 4.x
ubuntu@ubuntu:~/Downloads/opencv$ git show
commit cc1fbe0956832e104556fca286f5547df4dcac90 (HEAD -> 4.x, origin/master, origin/HEAD, origin/4.x)
Merge: 97c6ec6d49 fa222b0ea2
Author: Alexander Alekhin <alexander.a.alekhin@gmail.com>
Date:   Wed Nov 24 20:10:37 2021 +0000

    Merge pull request #21108 from ShuangLiu1992:patch-1

ubuntu@ubuntu:~/Downloads/opencv$ 

10.某日更新后

添加配置

QT配置

sudo make -j8 

sudo make install

交叉编译完成 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐