Linux下OpenCv+Qt搭配使用问题

2016年5月4日 由 Creater 留言 »

发现一个奇怪的现象,OpenCv代码在直接使用g++或者cmake+make编译后代码能正常跑出效果,但是以Qt组织工程后,任何涉及到OpenCv的代码都导致程序崩溃,比如segment error(段错误),relloc error或者the inferior has stopped because receive a signal的错误,经过多次实验排查,定位到问题。

现象:
使用原生的g++编译没问题

g++  -ggdb `pkg-config --cflags opencv` main.cpp -o main `pkg-config --libs opencv`  

使用Cmake没问题,CMakeLists.txt内容如下:

######## A simple cmakelists.txt file for OpenCV() #############
cmake_minimum_required(VERSION 2.8)
PROJECT(ShowImage)
FIND_PACKAGE( OpenCV REQUIRED )   
INCLUDE_DIRECTORIES( 
  ${ShowImage_SOURCE_DIR}
)
ADD_EXECUTABLE(ShowImage main.cpp)  
TARGET_LINK_LIBRARIES (ShowImage ${OpenCV_LIBS} )
cmake .
make

在QtCreator中,配置文件中加有

INCLUDEPATH+=/usr/local/include\
/usr/local/include/opencv\
/usr/local/include/opencv2
LIBS+=/usr/local/lib/libopencv_highgui.so\
/usr/local/lib/libopencv_core.so\
/usr/local/lib/libopencv_imgproc.so\
/usr/local/lib/libopencv_imgcodecs.so \
/usr/local/lib/libopencv_superres.so \
/usr/local/lib/libopencv_videoio.so \
/usr/local/lib/libopencv_calib3d.so \
/usr/local/lib/libopencv_video.so \
/usr/local/lib/libopencv_features2d.so \
/usr/local/lib/libopencv_videostab.so \
/usr/local/lib/libopencv_flann.so \
/usr/local/lib/libopencv_highgui.so \
/usr/local/lib/libopencv_imgcodecs.so \
/usr/local/lib/libopencv_imgproc.so \
/usr/local/lib/libopencv_ml.so \
/usr/local/lib/libopencv_objdetect.so 

但是程序总是停止运行。

解决办法:
首先

qmake -v
QMake version 2.01a
Using Qt version 4.8.6 in /usr/lib/x86_64-linux-gnu

可以看出当前该系统安装的Qt版本。在编译OpenCv时带的参数WITH_QT=ON选项会导致OpenCv编译时使用qt4,应该是这种关联导致冲突或者不匹配。
你现在可以这样做:

cd build
make uninstall
make clean
cd ..
rm -rf build

即卸载,清除,删除文件夹,现在只需要将opencv重新编译一遍即可,注意去掉WITH_QT=ON选项。

广告位

发表评论

你必须 登陆 方可发表评论.