OpenCV : undefined reference to imread()

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


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;

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