简体   繁体   中英

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

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

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

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

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.

I don't use the Qt-creator , I use Vim instead .So i created my own commands that work just fine . this is my cv2run custom command content :

 #!/bin/bash 

file=$@

if [ "${file: -4}" = ".cpp"  ]; then 
        g++ $file -I/opt/robots/pepper/ctc-linux64-atom-2.5.10.7/opencv2/include/ -L/opt/robots/pepper/ctc-linux64-atom-2.5.10.7/opencv2/lib -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_videoio 
    if $status ; then 
        ./a.out && rm -rf a.out
    fi
else
    echo "This Command should be used only with cpp files that uses the opencv library !!"
fi 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM