ubuntu12.04中opencv静态编译
1.12.04中没有了新立德,忍受不了软件中心的慢,所以安装了新立德
apt-get install synaptic
2.安装gcc等C/C++编程相关工具
sudo apt-get install build-essential
3.安装gtk+(opencv的界面显示就靠它了)
安装GTK环境只要安装一个libgtk2.0-dev就可以了,而安装gnome开发环境的话,需要装gnome-core-devel,它包含GTK开发包。
sudo apt-get install gnome-core-devel(有点慢,需要下载,看网速)
pkg-config --modversion gtk+ (查看1.2.x版本)
pkg-config --modversion gtk+-2.0 (查看 2.x 版本)
pkg-config --version (查看pkg-config的版本)
pkg-config --list-all |grep gtk (查看是否安装了gtk)
4.使用synaptic安装cmake-qt版本
有界面的cmake配置opencv编译选项方便些
5.安装wxWidgets(codeblocks IDE需要到)V2.8(详情见)
由于codeblocks是用wxWidgets开发的,所以wxWidgets需要先安装.以最小化体积为编译目的,shared,unicode版本,库文件编译为一个文件,不支持2.8,去掉debug信息,最终配置命令如下(./configure --help可查看参数信息):
./configure --enable-unicode --enable-shared--enable-monolithic --disable-compat28 --disable-debug_flag
make(需要一定时间)
make install
ldconfig(需要把/usr/local/lib加到/etc/ld.so.conf最后一行中,再执行ldconfig)
编译后的文件放在/usr/local/bin和/usr/local/lib目录中,可通过—prefix改变路径.
由于Codeblocks暂时只支持2.8,所以卸载2.9的:make uninstall.重新安装2.8
./configure --enable-unicode --enable-shared--enable-monolithic --disable-compat26 --disable-debug_flag
make(需要一定时间)
make install
6.安装CodeBlocks(Ver10.05)
Codeblocks最新版本10.05(截至2012-5-12)只能使用wxWidgets2.8,不兼容2.9.使用2.9会出现以下错误:editarrayorderdlg.h:20:error: default argument for parameter of type 'const wxArrayString&' hastype 'long int'. 且wxWidgets需编译成单一文件.源代码下的BUILD文件里面有编译说明.直接顺序执行以下三条命令就行.
./configure --with-contrib-plugins=all
make
make install
安装完成后,打开它只发现它在任务栏中闪一会儿就消失了,出不了界面.在终端得运行提示:codeblocks:error while loading shared libraries: libcodeblocks.so.0: cannot open sharedobject file: No such file or directory
解决办法为:In that case make sure the library path where the Code::Blocks librarieswhere installed into is "known" to the system. For example: On Ubuntuusing a default build process on a clean system will install the Code::Blocksexecutables to /use/local/bin and the libraries to /usr/local/lib. The latteris usually not known to a "clean" Ubuntu system. To add it to thesearch path for libraries do the following (as root / using sudo respectively):Add the following line to the file /etc/ld.so.conf:
/usr/local/lib
...and run:
ldconfig
That's it - Code::Blocks should now work just fine asall libraries are being found.
wxSmith用不了,打开CB提示:
/usr/local/lib/codeblocks/plugins/libwxSmithAui.so:not loaded (missing symbols?)
/usr/local/lib/codeblocks/plugins/libwxsmithcontribitems.so:not loaded (missing symbols?)
/usr/local/lib/codeblocks/plugins/libwxsmith.so: notloaded (missing symbols?)
运行 sudo ldconfig就行
7.安装ffmpeg(Ver0.10.3)
需要先安装ffmpeg(先安装yasm),下载源代码,
./configure --enable-shared(需要动态链接)
make
makeinstall
ldconfig四步就行.
如果机子以前安装过ffmpeg,需要先卸载干净。本人使用synaptic装过0.8版本,最后使用synaptic卸载了,但libav*等库文件还存在,所以怎样编译ffmpeg都是链接到0.8的那些库,都会是出错。
只要libavdevice,filter,format,codec,swresample,swscale,avutil这7个库中有链接到以下目录的,就是链接到了旧文件。
/usr/lib/i386-linux-gnu/,/usr/lib/i386-linux-gnu/i686/cmov/。应该链接到/usr/local/lib目录里才是正确的。
解决办法:
删除/usr/lib/i386-linux-gnu/,/usr/lib/i386-linux-gnu/i686/cmov/这两个目录里相应的文件,然后重新ldconfig,再ldd `which ffmpeg`查看ffmpeg库的链接情况,如果都链接到了/usr/local/lib目录里的相关库,则成功。不行则把上面2个目录里的相关库文件删除了再编译链接。
8.安装opencv(Ver2.4.2)
使用cmake编译,
取消BUILD_JASPER JPEG PNG TIFF ZLIB(linux的思想是共享,所以使用系统的图像库,没有则先安装)
BUILD_SHARED_LIBSBUILD_PERF_TESTS BUILD_TESTS(由于静态库,没有指定ffmpeg相关库文件,这些Tests会编译失败)
编译并安装完成后,使用pkg-config查看头文件与库文件信息
root@ubuntu:~# pkg-config opencv --cflags //查看包含目录
-I/usr/local/include/opencv -I/usr/local/include
root@ubuntu:~# pkg-config opencv --libs //查看库链接,可见该格式是错误的,没有-L与-l标识,所以需要自己添加
/usr/local/lib/libopencv_contrib.a/usr/local/lib/libopencv_legacy.a /usr/local/lib/libopencv_ml.a/usr/local/lib/libopencv_stitching.a /usr/local/lib/libopencv_ts.a/usr/local/lib/libopencv_videostab.a /usr/local/lib/libopencv_gpu.a/usr/local/lib/libopencv_nonfree.a /usr/local/lib/libopencv_objdetect.a/usr/local/lib/libopencv_calib3d.a /usr/local/lib/libopencv_photo.a/usr/local/lib/libopencv_video.a /usr/local/lib/libopencv_features2d.a/usr/local/lib/libopencv_highgui.a/usr/local/share/OpenCV/3rdparty/lib/liblibjasper.a/usr/local/share/OpenCV/3rdparty/lib/liblibtiff.a/usr/local/share/OpenCV/3rdparty/lib/liblibpng.a/usr/local/share/OpenCV/3rdparty/lib/liblibjpeg.a/usr/local/lib/libopencv_flann.a /usr/local/lib/libopencv_imgproc.a/usr/local/lib/libopencv_core.a /usr/local/share/OpenCV/3rdparty/lib/libzlib.aswscale avformat avutil z avcodec gstvideo-0.10 gstapp-0.10 xml2 gmodule-2.0gstreamer-0.10 gstbase-0.10 gthread-2.0 glib-2.0 gobject-2.0 fontconfigfreetype pango-1.0 cairo gdk_pixbuf-2.0 pangocairo-1.0 pangoft2-1.0 gio-2.0atk-1.0 gdk-x11-2.0 gtk-x11-2.0 rt pthread m dl stdc++
9.测试opencv
头文件位置设置:codeblocks中 项目—Build option-Compiler settings--otheroptions 中加入`pkg-config opencv --cflags`
库链接参数设置:codeblocks中 项目—Build option-Linker settings--other linkeroptions 中加入`pkg-config opencv --libs`,由前面可知静态编译完成后的opencv得到的libs格式不正确,所以需要手动改为正确格式再输出otherlinker options中(具体参数见下表分析)。
包 |
静 |
链 |
-static -lopencv_contrib -lopencv_legacy -lopencv_ml -lopencv_stitching -lopencv_videostab -lopencv_gpu -lopencv_nonfree -lopencv_objdetect -lopencv_calib3d -lopencv_photo -lopencv_video -lopencv_features2d -lopencv_highgui -llibjasper -llibtiff -llibpng -llibjpeg -lopencv_flann -lopencv_imgproc -lopencv_core -lswscale -lavformat -lavcodec -lavutil -lva -lzlib -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lglib-2.0 -lgthread-2.0 -lrt -lpthread -lm -ldl -lstdc++ |
链接不成功 |
生成 |
链接不成功: |
|||
动 |
链 |
-lopencv_contrib -lopencv_legacy -lopencv_ml -lopencv_stitching -lopencv_videostab -lopencv_gpu -lopencv_nonfree -lopencv_objdetect -lopencv_calib3d -lopencv_photo -lopencv_video -lopencv_features2d -lopencv_highgui -llibjasper -llibtiff -llibpng -llibjpeg -lopencv_flann -lopencv_imgproc -lopencv_core -lswscale -lavformat -lavcodec -lavutil -lva -lzlib -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lglib-2.0 -lgthread-2.0 -lrt -lpthread -lm -ldl -lstdc++ |
成功,可运行 |
|
生 |
linux-gate.so.1 libva.so.1 libgtk-x11-2.0.so.0 libgdk-x11-2.0.so.0 libgobject-2.0.so.0 libglib-2.0.so.0 librt.so.1 libpthread.so.0 libstdc++.so.6 libm.so.6 libgcc_s.so.1 libc.so.6 libdl.so.2 libpangocairo-1.0.so.0 libX11.so.6 libXfixes.so.3 libatk-1.0.so.0 libcairo.so.2 libgdk_pixbuf-2.0.so.0 libgio-2.0.so.0 libpangoft2-1.0.so.0 libpango-1.0.so.0 libfontconfig.so.1 libXext.so.6 libXrender.so.1 libXinerama.so.1 libXi.so.6 libXrandr.so.2 libXcursor.so.1 libXcomposite.so.1 libXdamage.so.1 ibffi.so.6 libpcre.so.3 /lib/ld-linux.so.2 libfreetype.so.6 libxcb.so.1 libpixman-1.so.0 libpng12.so.0 libxcb-shm.so.0 libxcb-render.so.0 libz.so.1 libgmodule-2.0.so.0 libselinux.so.1 libresolv.so.2 libexpat.so.1 libXau.so.6 libXdmcp.so.6 |
|||
不 |
静 |
链 |
-static -L/usr/local/lib -L/usr/local/share/OpenCV/3rdparty/lib |
成功,可以运行 |
生成文件依赖项 |
目标程序没有链接动态库not a dynamic executable,即不依赖于任何文件,比MS牛 |
|||
动 |
链 |
-shared -L/usr/local/lib -L/usr/local/share/OpenCV/3rdparty/lib |
运行不了,提示Illegal instruction (core dumped) |
|
生成 |
linux-gate.so.1 => (0x00f82000) |
出错处理:
/usr/bin/ld: cannot find -lgstvideo-0.10
/usr/bin/ld: cannot find -lgstapp-0.10
/usr/bin/ld: cannot find -lgdk_pixbuf-2.0
/usr/bin/ld: cannot find -latk-1.0
(使用-static参数就会出现上面的错误)把库链接参数中的-lgstvideo-0.10-lgstapp-0.10 -lgdk_pixbuf-2.0 -latk-1.0四项去掉(每项对应一个错误)