Search code examples
c++eclipseopencveclipse-cdt

Cannot compile because of undefined references?


I'm tying to run a simple OpenCV code, but unfortunately I'm getting an undefined reference error. And I really don't know what to do. This is the code that I'm trying to make it run

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main(int argc, char** argv) {

    Mat image;

    image = imread(argv[1], 1);
    //...
    return 0;
}

And this is the eclipse output: basically it says: undefined reference to cv::imread(cv::String const&, int)'

The complete error message is:

g++ -L/usr/local/lib -o "TestOpenCV"  ./src/TestOpenCV.o   -lopencv_core -lopencv_highgui
./src/TestOpenCV.o: In function `main':
/media/cip/Media/workspace/OpenCV/TestOpenCV/Debug/../src/TestOpenCV.cpp:21: undefined reference to cv::imread(cv::String const&, int) collect2: error: ld returned 1 exit status make: *** [TestOpenCV] Error 1

And the list output of pkg-config opencv --libs looks like this:

/usr/local/lib/libopencv_calib3d.so 
/usr/local/lib/libopencv_core.so 
/usr/local/lib/libopencv_features2d.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 
/usr/local/lib/libopencv_photo.so 
/usr/local/lib/libopencv_shape.so 
/usr/local/lib/libopencv_stitching.so 
/usr/local/lib/libopencv_superres.so 
/usr/local/lib/libopencv_ts.a 
/usr/local/lib/libopencv_video.so 
/usr/local/lib/libopencv_videoio.so 
/usr/local/lib/libopencv_videostab.so 
/usr/local/lib/libopencv_viz.so 

Solution

  • There is similar question already. Try adding

    -lopencv_imgcodecs 
    

    to the list of linker flags.