OpenCV : undefined reference to imread()

46,044

Solution 1

Since OpenCV3, the imread function resides in the imgcodecs module. Imread should work once you add the opencv_imgcodecs library to your project (note: imgcodecs, not imcodecs).

Solution 2

I recommend to link the following libraries:

opencv_core
opencv_highgui
opencv_imgproc
opencv_imgcodecs

And in the .cpp file, you can include like this

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

    using namespace std;
    using namespace cv;

Or

    #include <iostream>
    #include <opencv2/opencv.hpp>

    using namespace std;
    using namespace cv;

Solution 3

This function is located in opencv_imgcodecs library. It also worths mentioning that you may need to put your object file before libraries in order to link successfully:

g++ -c -I/usr/include/opencv4/opencv -I/usr/include/opencv4 main.cpp
g++ main.o -lopencv_imgcodecs $(OTHER_FLAGS) -o main
Share:
46,044

Related videos on Youtube

Shinchan
Author by

Shinchan

Updated on July 09, 2022

Comments

  • Shinchan
    Shinchan almost 2 years

    I have configured OpenCV 3.1.0 in Eclipse Mars. These are my configuration,

    G++ includes: D:/opencv/build/install/include; GCC includes: D:/opencv/build/install/include

    Linker libraries: libopencv_core310, libopencv_highgui310

    Linker libraries path: D:/opencv/build/lib (files in this directory are like libopencv_core310.dll.a)

    I am getting an error like this,

    imageRead.cpp:15: undefined reference to `cv::imread(cv::String const&, int)'
    

    This is my imageRead.cpp file,

    #include <iostream>
    #include "opencv2/core/core.hpp"
    #include "opencv2/highgui/highgui.hpp"
    
    using namespace std;
    using namespace cv;
    
    int main(int argc, const char** argv) {
        Mat img = imread("D:/sample.jpg", CV_LOAD_IMAGE_UNCHANGED);
        if (img.empty()) {
            cout << "Error: Image cannot be loaded." << endl;
            system("pause");
            return -1;
        }
        namedWindow("Image Window", CV_WINDOW_AUTOSIZE);
        imshow("Image Window", img);
        if (waitKey() == 27) {
            return -1;
        }
        destroyWindow("Image Window");
        return 0;
    }
    

    Can anyone help with this error ?

    • Shinchan
      Shinchan over 8 years
      It says "Error starting process: Cannot run program"
    • berak
      berak over 8 years
      that's unrelated to your original question.
    • Sagar Patel
      Sagar Patel over 8 years
      Try by adding opencv_highgui in linker libray.
  • Juan David Torres
    Juan David Torres about 3 years
    The actual code is #include <opencv2/imgcodecs.hpp>