error: undefined reference to `cv::imread(std::string const&, int)'

Moonlight picture Moonlight · Aug 24, 2014 · Viewed 23.1k times · Source

I'm new to Qt and I have a project that needs to configure OpenCV in Qt , I tried to run a simple code in Qt but I got this error "undefined reference to cv::imread(std::string const&, int)" and here is my code...

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;

int main(){
Mat src,gray;
src=imread("C:/Users/user/Desktop/wood-pattern.png",1);
imshow("gg",src);
cvWaitKey(0);
return 0;
}

and my source.pro is

#-------------------------------------------------
#
# Project created by QtCreator 2014-08-24T20:38:56
#
#-------------------------------------------------
INCLUDEPATH += C:\opencv\opencv2.4.9\build\include\
CONFIG(release,debug|release)
{
LIBS += C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_calib3d249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_contrib249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_core249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_features2d249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_flann249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_gpu249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_highgui249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_imgproc249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_legacy249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_ml249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_objdetect249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_ts249.lib \
        C:\opencv\opencv2.4.9\build\x86\vc11\lib\opencv_video249.lib
        }
CONFIG(debug,debug|release)
       {
LIBS += C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_calib3d249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_contrib249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_core249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_features2d249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_flann249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_gpu249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_highgui249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_imgproc249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_legacy249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_ml249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_objdetect249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_ts249.lib \
        C:\opencv\opencv2.4.9\build\x64\vc11\lib\opencv_video249.lib \
}
QT       += core

QT       -= gui

TARGET = Source
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

I saw all the answers about but they didn't work. my Qt is 32bit and my Windows is 64bit. I think my configuration is wrong. I followed many tutorial but they weren't for OpenCV 2.4.9 so please help me ...

Answer

Alexey Shmalko picture Alexey Shmalko · Aug 24, 2014

See output of pkg-config opencv --libs to find out what libraries you're missing, then add them to your config.

For me, the full list of libraries is

/usr/lib64/libopencv_calib3d.so.2.4.8
/usr/lib64/libopencv_contrib.so.2.4.8
/usr/lib64/libopencv_core.so.2.4.8
/usr/lib64/libopencv_features2d.so.2.4.8
/usr/lib64/libopencv_flann.so.2.4.8
/usr/lib64/libopencv_gpu.so.2.4.8
/usr/lib64/libopencv_highgui.so.2.4.8
/usr/lib64/libopencv_imgproc.so.2.4.8
/usr/lib64/libopencv_legacy.so.2.4.8
/usr/lib64/libopencv_ml.so.2.4.8
/usr/lib64/libopencv_nonfree.so.2.4.8    # you don't have this one
/usr/lib64/libopencv_objdetect.so.2.4.8
/usr/lib64/libopencv_photo.so.2.4.8      # this one
/usr/lib64/libopencv_stitching.so.2.4.8  # this one
/usr/lib64/libopencv_superres.so.2.4.8   # and this one
/usr/lib64/libopencv_ts.a
/usr/lib64/libopencv_video.so.2.4.8