零、环境准备:
编译环境:Ubuntu-14.04
交叉编译工具链:arm-linux-gnueabi-4.5.1
目标机器:粤嵌GEC210开发板S5PV210(Cortex-a8)
opencv库:opencv-3.2.0(源码),解压至某一目录
opencv_contrib库:opencv_contrib-3.2.0(扩展库源码),解压至某一目录
cmake及cmake-gui编译工具:若未安装,请执行
$ sudo apt-get install cmake
$ sudo apt-get install cmake-qt-gui
其他依赖库:
$ sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libtiff4-dev libswscale-dev libjasper-dev
一、cmake-gui界面配置opencv,并生成Makefile
cmake的主要作用是生成Makefile,opencv交叉编译需要借助cmake来生成Makefile。
1、任一目录下,进入cmake-gui界面:
$ sudo cmake-gui
source code源码:选择解压出来的opencv目录
build the binaries编译目录:自行创建一个目录,如我在opencv源码下创建一个build-arm
2、配置生成目标选项
点击 Configure ,配置如图:
配置两项: Unix Makefiles 、 Specify options for cross-compiling
我们最终要的是linux Makefile,交叉编译cross-compling
点击 Next
3、配置编译器
配置如图:
Operating System操作系统:arm-linux
编译器 C:选择交叉编译器的gcc,如我的 arm-linux-gcc
编译器 C++:选择交叉编译器的g++,如我的 arm-linux-g++
Target Root:选择交叉编译器的bin目录
完成,点击 Finish
4、指定编译的内容、路径等
勾上 BUILD_JPEG 和 BUILD_JNP (此2项可选)
重要一项---配置安装目录:
默认是/usr/local,这样会用交叉编译后的库替换了原有的库,且不可用。
最好自行创建一个目录,方便管理。如我将安装在 /usr/local/arm/opencv-arm
5、执行配置,生成Makefile
配置完成,再次点击 Configure,点 Generate,如图:
configuring done , generating done ,应该已经生成Makefile了,完成则关闭界面。
二、编译、安装opencv
1、查看是否已生成Makefile
进入编译目录(即前面配置的“build the binaries编译目录”),可看到刚刚生成的Makefile文件:
$ cd /root/library/opencv/opencv-3.2.0/build-arm/
2、执行编译,并静候出错:
$ make
如愿以偿,出现以下错误:
错误1: 没有链接到 pthread 库
../../lib/libopencv_core.so: undefined reference to `pthread_mutexattr_destroy'
../../lib/libopencv_core.so: undefined reference to `pthread_create'
../../lib/libopencv_core.so: undefined reference to `dlopen'
../../lib/libopencv_core.so: undefined reference to `pthread_mutex_trylock'
../../lib/libopencv_core.so: undefined reference to `clock_gettime'
../../lib/libopencv_core.so: undefined reference to `dlsym'
../../lib/libopencv_core.so: undefined reference to `pthread_mutexattr_settype'
../../lib/libopencv_core.so: undefined reference to `pthread_join'
../../lib/libopencv_core.so: undefined reference to `pthread_mutexattr_init'
collect2: ld returned 1 exit status
make[2]: *** [bin/opencv_perf_core] 错误 1
make[1]: *** [modules/core/CMakeFiles/opencv_perf_core.dir/all] 错误 2
make: *** [all] 错误 2
解决1:
修改编译目录下的 CMakeCache.txt 文件:
找到 CMAKE_EE_LINKER_FLAGS:STRING 项:
190 //Flags used by the linker.
191 CMAKE_EXE_LINKER_FLAGS:STRING=' '
修改成以下:
190 //Flags used by the linker.
191 CMAKE_EXE_LINKER_FLAGS:STRING= -pthread -lrt
修改完成,继续 make
等待出错......
错误2: 没有链接到 dl 库
[ 49%] Built target opencv_ts
Linking CXX executable ../../bin/opencv_perf_core
../../lib/libopencv_core.so: undefined reference to `dlopen'
../../lib/libopencv_core.so: undefined reference to `dlsym'
collect2: ld returned 1 exit status
make[2]: *** [bin/opencv_perf_core] 错误 1
make[1]: *** [modules/core/CMakeFiles/opencv_perf_core.dir/all] 错误 2
make: *** [all] 错误 2
解决2:
同样是 CMAKE_EE_LINKER_FLAGS:STRING ,继续添加 -ldl
190 //Flags used by the linker.
191 CMAKE_EXE_LINKER_FLAGS:STRING= -pthread -lrt -ldl
再make........
错误3:
用arm-2009q3(4.4.1)编译工具链编译时,还会出现以下错误(4.5.1无此报错)
/root/library/opencv/opencv-3.2.0/modules/imgproc/src/drawing.cpp:1026: error: call of overloaded 'abs(long long int&)' is ambiguous
/usr/local/arm/arm-2009q3/bin/../arm-none-linux-gnueabi/libc/usr/include/stdlib.h:720: note: candidates are: int abs(int)
解决3:
百度解决方案,将错误处的drawing.cpp文件中的 abs()函数改为 fabs()
再再make ...
错误4:
用arm-2009q3(4.4.1)编译工具链编译时,还会出现以下错误(4.5.1无此报错)
Linking CXX executable ../../bin/opencv_perf_core
/usr/local/arm/arm-2009q3/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/bin/ld: ../../bin/opencv_perf_core: hidden symbol `__sync_fetch_and_add_4' in /usr/local/arm/arm-2009q3/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.1/libgcc.a(linux-atomic.o) is referenced by DSO
/usr/local/arm/arm-2009q3/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/bin/ld: final link failed: Nonrepresentable section on output
collect2: ld returned 1 exit status
make[2]: *** [bin/opencv_perf_core] 错误 1
make[1]: *** [modules/core/CMakeFiles/opencv_perf_core.dir/all] 错误 2
make: *** [all] 错误
解决4:
无解。。。。。。暂未找到对应办法
建议换个高版本的编译工具链吧。
相同配置,arm-2009q3(4.4.1)比4.5.1多了几个怪怪的问题。
make OK .
3、安装:
$ make install
顺利!!!
看下安装好的库,在安装目录下有这几个文件夹:bin include lib LICENSE pkgconfig share
其中lib下才是我们想要的库(动态库,亦可配置编译静态库),要移植到开发板上的对象。
三、编译、安装扩展库 opencv_contrib
先将opencv_contrib-3.2.0源码下载,解压。
1、打开cmake-gui界面:
$ cmake-gui
找到 OPENCV_EXTRA_MODULES_PATH 项,选择扩展库源码 opencv_contrib-3.2.0 目录下的 modules 目录
点击 Configure ,生成配置(若仍然有红色,则再点多次至红色消失)
点击 Generate ,生成 Makefile
2、编译、安装
$ make
$ make install
这两步异常的顺利。
看下多了什么库,注意安装前后对比:
搞定,收工???
下篇继续吧。
参考博客:
https://blog.csdn.net/gatieme/article/details/49080355
https://blog.csdn.net/luotuo44/article/details/8958990