I'm facing the apparently very typical undefined reference with OpenCV linking with g++. I'm running in two machines, both with opencv/2.3.14.6 installed, linux OS, same code to compile, using pkg-config. I checked 'pkg-config opencv --libs' and in both is the same result
-L/usr/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lrt -lpthread -lm -ldl
The machine where it fails has CentOS Linux release 7.9.2009 (will call it centos machine), the one were compilation passes Linux version 4.19.0-9-amd64 (will call it debian machine).
In my code, I use #include <opencv2/opencv.hpp>
, which should include all opencv libs, however, I tried also including the specific libs, same result, debian machine all good, centos machine fails. I even tried running one of the examples from OpenCV documentation where most of the functions I get flagged are being used, got the same result. This is part of the error stack, same error goes for imread, imshow, namedwindow and videowriter, everything else seems fine:
test.cpp:(.text+0x1af4): undefined reference to `cv::putText(cv::Mat&, std::cxx11::basic_string<char, std::char_traits, std::allocator > const&, cv::Point, int, double, cv::Scalar, int, int, bool)'
I checked and all those functions do exist in the libs being included, for example, putText is contained in opencv2/core/core.hpp
CV_EXPORTS_W void putText( Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false );
This is the command line I'm using to compile
g++ -Wall -std=c++11 -o test test.cpp `pkg-config --cflags --libs opencv`
Also tried this next one, just in case
g++ -Wall -std=c++11 -o test test.cpp `pkg-config opencv --cflags --libs`
And this
g++ -Wall -std=c++11 -o test test.cpp $(pkg-config opencv --cflags --libs)
Also tried replacing pkg-config opencv --cflags --libs
with the direct libs list, still not able to compile in centos machine, and I do need to run in centos machine, but am unable to, most posts I've checked are solved with adding missing pkg-config opencv --cflags --libs
to command line, code syntax error in files to compile, missing file or reinstalling opencv, but none of those help me. Also tried using different gcc versions as recommended here, but same result. I wonder if there is something else I can try or if I'm doomed to use debian machine (centos machine is an HPC machine, code takes a week to run in debian machine!).
EDIT: for anyone having similar issues, see comments and check dual ABI, in my case, I was trying to use external libraries compiled with different gcc versions, solution is to ensure they are all compiled using compatible compiler versions.