Undefined reference to imread: -l and pkg-config not working

I’m getting linker errors on code that uses imread, imshow, and the Mat object constructor and destructor.

I’m on Linux Mint version 20.3, my OpenCV version is 4.2, and my g++ version is 9.4.

This this the output from linking the libraries one by one:

g++ -L/usr/lib/x86_64-linux-gnu -lopencv_core -lopencv_imgcodecs -lopencv_highgui main.o visualizeBitPallette.o -o visualizeBitPallette

/usr/bin/ld: main.o: in function `grayscaleRedTest()':
main.cpp:(.text+0x449): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: main.cpp:(.text+0x509): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: main.cpp:(.text+0x542): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: main.cpp:(.text+0x551): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: main.cpp:(.text+0x60a): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: main.cpp:(.text+0x622): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: visualizeBitPallette.o: in function `getPreviewImage(std::vector<BitColor, std::allocator<BitColor> >, unsigned int, cv::Mat&)':
visualizeBitPallette.cpp:(.text+0x3a4): undefined reference to `cv::Mat::Mat(int, int, int)'
/usr/bin/ld: visualizeBitPallette.cpp:(.text+0x4d5): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:9: visualizeBitPallette] Error 1

Running “g++ pkg-config opencv4 --cflags --libs main.o visualizeBitPallette.o -o visualizeBitPallette” gives exactly the same output, as does linking with “pkg-config opencv4 --cflags --libs-L/usr/lib/x86_64-linux-gnu”.

The output from “pkg-config opencv4 --cflags --libs”:

-I/usr/include/opencv4/opencv -I/usr/include/opencv4 -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_ml -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

These are all of the libraries in the directory specified:

(cd /usr/lib/x86_64-linux-gnu && ls -1 libopencv*.so)

libopencv_aruco.so
libopencv_bgsegm.so
libopencv_bioinspired.so
libopencv_calib3d.so
libopencv_ccalib.so
libopencv_core.so
libopencv_datasets.so
libopencv_dnn_objdetect.so
libopencv_dnn.so
libopencv_dnn_superres.so
libopencv_dpm.so
libopencv_face.so
libopencv_features2d.so
libopencv_flann.so
libopencv_freetype.so
libopencv_fuzzy.so
libopencv_hdf.so
libopencv_hfs.so
libopencv_highgui.so
libopencv_imgcodecs.so
libopencv_img_hash.so
libopencv_imgproc.so
libopencv_line_descriptor.so
libopencv_ml.so
libopencv_objdetect.so
libopencv_optflow.so
libopencv_phase_unwrapping.so
libopencv_photo.so
libopencv_plot.so
libopencv_quality.so
libopencv_reg.so
libopencv_rgbd.so
libopencv_saliency.so
libopencv_shape.so
libopencv_stereo.so
libopencv_stitching.so
libopencv_structured_light.so
libopencv_superres.so
libopencv_surface_matching.so
libopencv_text.so
libopencv_tracking.so
libopencv_videoio.so
libopencv_video.so
libopencv_videostab.so
libopencv_viz.so
libopencv_ximgproc.so
libopencv_xobjdetect.so
libopencv_xphoto.so

My included OpenCV headers:

#include <opencv2/core/mat.hpp>

#include <opencv2/imgcodecs.hpp> // imread
#include <opencv2/highgui.hpp>   // imshow

How can I successfully link this code against the libraries I need?

it cannot link opencv_core.

which os / g++ / opencv version is it ?
how did you install / build the opencv libs ?

-L/usr/lib/x86_64-linux-gnu

that’s a weird place. default would be /usr/local/lib

they stopped supporting pkg-config a while ago in favour of cmake

maybe it’s just a matter of adding -std=c++11 to the cmdline ?

I’m on Linux Mint version 20.3, my OpenCV version is 4.2, and my g++ version is 9.4. I’ve updated my original post to reflect this.

I installed with the following steps (running these commands again did not help):
cd opencv-build
cmake …/opencv-4.x
cmake --build .

1 Like