error: undefined reference to `cv::imread(std::str

2019-02-25 06:18发布

问题:

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 ...

回答1:

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


回答2:

No idea about Windows, but if it has pkg-config, you should be able to get QMake to use it automatically by adding

PKGCONFIG += opencv

to your project file (assuming that your OpenCV installs the correct package-config files as it does on sane platforms).



回答3:

In my case it was solely the order of parameters, note that

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

works while

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

doesn't since the latter defines what main.cpp needs before main.cpp is referenced.