"undefined reference" when linking against libopencv_world 4.5.3

I have built opencv 4.5.3 from source on my Ubuntu 18.04 machine with opencv_world option enabled. But I can’t link against it as I am getting the following link error-

$ g++ -std=c++11 hello_opencv.cpp -L/usr/local/lib -lopencv_world -o main
/tmp/ccxGz7bF.o: In function `main':
hello_opencv.cpp:(.text+0x57): undefined reference to `cv::imread(cv::String const&, int)'
hello_opencv.cpp:(.text+0xd0): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/tmp/ccxGz7bF.o: In function `cv::String::String(char const*)':
hello_opencv.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccxGz7bF.o: In function `cv::String::~String()':
hello_opencv.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccxGz7bF.o: In function `cv::String::operator=(cv::String const&)':
hello_opencv.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status

I have built the lib several times even with GLIBCXX_USE_CXX11_ABI=0 option which is mentioned in this issue.

Does it mean the opencv_world is broken, or am I missing something here?

  • is there an opencv_world.a in /usr/local/lib ?
  • if so, does the timestamp match your build ?
  • did you run a proper make install ?
  • you’re not setting any include path, like -I /usr/local/include/opencv4
    (how did this not error ?)
  • try building single module libs instead (esp. with contrib modules)

The issue has been resolved. It was indeed an error of include path which I thought was being passed implicitly by the IDE. Thanks for bringing my attention to it.