1、前期准备工作
a. 安装交叉编译链
我使用的是arm-linux-gcc 4.3.2,解压到目录/usr/local/arm/4.3.2,然后在环境变量PATH中增加/usr/local/arm/4.3.2/bin
b. 安装CMake
Opencv 2.0之后的版本,必须使用CMake创建Makefile。我使用的CMake版本是2.8.2,解压到目录/usr/local/cmake-2.8.2-Linux-i386,然后在环境变量PATH中增加/usr/local/cmake-2.8.2-Linux-i386/bin
2、编译Opencv
1、解压Opencv 2.4.9
2、创建build目录,作为CMake编译arm版本的工作目录
3、在X环境下,运行cmake-gui(如果没装的话,按照提示安装一个)
选择源代码目录
选择Build目录:
点击Configure,保持generator为Unix Makefiles,选择Specify options for cross-compiling,点击Next
Operating System填写arm-inux
点击NEXT再跳出的界面中做如下设置
Operating System 选择目标系统arm-linux
Compilers中选择交叉编译器的gcc和g++
Target Root选择交叉编译器的路径
程序库的Target Root填写/usr/local/arm/,然后点击Finish
点击Finish按钮,开始生成配置文件,但是这个时候会报错
System is unknown to cmake, create:
Platform/arm-linux to use this system, please send your config file to [email protected] so it can be added to cmake
这个是因为Operating System填写的arm-linux不算Cmake能识别的目标系统,这里可以忽略这个错误。
但是这个时候还有其他错误
CMake Error at cmake/FindCUDA.cmake:763 (if):
if given arguments:
"CUDA_VERSION" "VERSION_GREATER" "5.0" "AND" "CMAKE_CROSSCOMPILING" "AND" "MATCHES" "arm"
Unknown arguments specified
Call Stack (most recent call first):
cmake/OpenCVDetectCUDA.cmake:26 (find_package)
cmake/OpenCVFindLibsPerf.cmake:24 (include)
CMakeLists.txt:468 (include)
应该是我的电脑里没有安装CUDA的库,因为后面的开发中也不会用到CUDA,所以我将 WITH_CUDA的钩去掉,然后再点击一次Configure按钮,看看还有没有提示缺少哪些第三方库,网上其他文章说WITH_TIFF要去掉,但是这里没有报错就不管它了。同时可以将CMAKE_INSTALL_PREFIX改成你想要的路径,这个路径所opencv最后库文件和头文件的安装路径。在不存在其他问题后点击Generate生存Makefile文件,如果前面第三方的问题没有解决这里是会报错而无法生存Makefile。生成的Makefile文件和Cmake配置文件都在
Wher to build the binarier 中设置的文件夹下
修改默认配置,默认安装目录为/usr/local,由于我之前有装x86版的opencv,所以对于交叉编译的库来说并不合适,所以我把CMAKE_INSTALL_PREFIX变量改为/usr/local/arm/
4、 点击Generate生成Makefile
(1)如果你现在make,是肯定会出错的!(不修改,可能白等了至少15分钟,前车之鉴)
../../lib/libopencv_core.so: undefined reference to `pthread_spin_init‘
../../lib/libopencv_core.so: undefined reference to `pthread_spin_unlock‘
../../lib/libopencv_core.so: undefined reference to `pthread_spin_lock‘
../../lib/libopencv_core.so: undefined reference to `pthread_spin_destroy‘
../../lib/libopencv_core.so: undefined reference to `pthread_once‘
../../lib/libopencv_core.so: undefined reference to `clock_gettime‘
../../lib/libopencv_core.so: undefined reference to `pthread_spin_trylock‘
原因是找不到pthread链接库,打开文件夹下的CmakeCache.txt进行修改
//Flags used by the linker.(大概在194行附近)
CMAKE_EXE_LINKER_FLAGS:STRING=-lpthread -lrt
(2) opencv编译OPENEXR时要用到libImath.so,
在配置界面要把build_opencvxr和with_opencvxr去掉,否则会出现这个问题。
/usr/lib/libImath.so: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
make[2]: *** [lib/libopencv_highgui.so] 错误 1
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] 错误 2 make: *** [all] 错误 2
还要去掉build_opencv_ocl和with_opencl,否则会出下面的错误
../../lib/libopencv_ocl.so:undefined reference to ‘dlopen‘
还要去掉tiff库的编译
5、make
6、sudo make install
3、测试opencv库是否安装正确
#include "cv.h" #include"highgui.h" const char*picture = "lena.jpg"; int main(int argc,char** argv) { IplImage *pImage=cvLoadImage(picture,1); cvNamedWindow("Test",CV_WINDOW_AUTOSIZE); cvShowImage("Test",pImage); cvWaitKey(0); cvReleaseImage(&pImage); cvDestroyWindow("Test"); }
all:test CFLAGS=-Wall OPENCV_INCLUDE=-I /usr/local/arm/include/opencv CC = arm-linux-g++ OPENCV_LINK =-L/usr/local/arm/lib -lcv -lcxcore -lhighgui -lpthread -lrt %:%.cpp #$(CC) `pkg-config opencv --libs` $(OPENCV_INCLUDE) $< -o [email protected] $(OPENCV_LINK) $(CC) $(OPENCV_INCLUDE) $< -o [email protected] $(OPENCV_LINK) .PHONY:clean clean: rm -f test
4、移植到mini2440开发板上
首先复制主机/usr/local/arm/lib//下面的全部.so文件到嵌入式根文件系统的/lib/目录下,再复制我们编译的test到嵌入式系统/opt/myworks/目录下(并确保文件test属性为可执行),如果test可正常运行没有报告缺少库文件,说明我们编译的arm-linux版OpenCV库已经可以正常使用。