Undefined reference to `cv::cvtColor' even though library libopencv_imgproc is linked.

Hi,

I know there is a lot of similar questions on the Internet, I just can't figure out this on my own. Do I have wrong linking order?

g++ ../../source/Main.cpp ../../source/EncoderTests.cpp ../../../../TestHelpers/source/TestHelpers.cpp ../../../source/Encoder.cpp -std=c++17 -g -Wall -O0 -v -o ../../bin/Encoder.out -I../../../../../Library/Catch2/single_include/catch2/ -I../../../../TestHelpers/include -I../../../include -I/usr/local/include/opencv4 -L/usr/local/lib/opencv4/3rdparty -lavformat -lavcodec -lavutil -lswscale -lswresample -lx264 -lpthread -lm -lz -lstdc++fs -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -ldl -littnotify -lippiw -lippicv -lIlmImf -llibjpeg-turbo -llibjasper -lpng -llibtiff -llibwebp


Here are the error messages:


/usr/bin/ld: //usr/local/lib/libopencv_imgcodecs.a(loadsave.cpp.o): in function `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
loadsave.cpp:(.text._ZN2cv6imreadERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi+0x432): undefined reference to `cv::resize(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, double, double, int)'
/usr/bin/ld: //usr/local/lib/libopencv_imgcodecs.a(grfmt_hdr.cpp.o): in function `cv::HdrEncoder::write(cv::Mat const&, std::vector<int, std::allocator<int> > const&)':
grfmt_hdr.cpp:(.text._ZN2cv10HdrEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x1b2): undefined reference to `cv::merge(cv::_InputArray const&, cv::_OutputArray const&)'
/usr/bin/ld: //usr/local/lib/libopencv_imgcodecs.a(grfmt_jpeg2000.cpp.o): in function `cv::Jpeg2KDecoder::readData(cv::Mat&)':
grfmt_jpeg2000.cpp:(.text._ZN2cv13Jpeg2KDecoder8readDataERNS_3MatE+0x6fc): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
/usr/bin/ld: //usr/local/lib/libopencv_imgcodecs.a(grfmt_pfm.cpp.o): in function `cv::PFMDecoder::readData(cv::Mat&)':
grfmt_pfm.cpp:(.text._ZN2cv10PFMDecoder8readDataERNS_3MatE+0x30f): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
/usr/bin/ld: //usr/local/lib/libopencv_imgcodecs.a(grfmt_tiff.cpp.o): in function `cv::TiffDecoder::readData_32FC3(cv::Mat&)':
grfmt_tiff.cpp:(.text._ZN2cv11TiffDecoder14readData_32FC3ERNS_3MatE+0x145): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
/usr/bin/ld: grfmt_tiff.cpp:(.text._ZN2cv11TiffDecoder14readData_32FC3ERNS_3MatE+0x176): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
/usr/bin/ld: //usr/local/lib/libopencv_imgcodecs.a(grfmt_tiff.cpp.o): in function `cv::TiffEncoder::write_32FC3(cv::Mat const&)':
grfmt_tiff.cpp:(.text._ZN2cv11TiffEncoder11write_32FC3ERKNS_3MatE+0xb4): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
/usr/bin/ld: //usr/local/lib/libopencv_imgcodecs.a(grfmt_webp.cpp.o):grfmt_webp.cpp:(.text._ZN2cv11WebPDecoder8readDataERNS_3MatE+0x3d8): more undefined references to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)' follow
collect2: error: ld returned 1 exit status


What I don't get is why libopencv_imgcodecs.a is missing references even though they shoud have been included in libopencv_imgproc.a which is linked just before libopencv_imgcodecs.

What is wrong?

Thank you for any help.

Last edited on
Maybe I am getting this error because I've compiled opencv library without -std=c++17 and that causes difference in ABI?
The order does matter.

> What I don't get is why libopencv_imgcodecs.a is missing references even though they shoud
> have been included in libopencv_imgproc.a which is linked just before libopencv_imgcodecs.

> -lopencv_imgproc -lopencv_imgcodecs
If, at the point the linker scans opencv_imgproc to see if it matches ANY of the currently known unresolved symbols, it finds no matches, then the library will not be included.

The symbol has to be unresolved at the point the library is being scanned.
If it becomes unresolved later, then you either need to
- reorder the libraries into a better search order
- list the same library twice to break a cyclic dependency

https://stackoverflow.com/questions/4802624/linking-libraries-that-contain-circular-references-in-gcc
Or make use of the start-group / end-group feature of the linker, which will repeatedly scan a list of libraries until the grouped list of libraries stops resolving more symbols.
For those who want to understand deeper, Eli Bendersky wrote a quite readable piece on static linking order: https://eli.thegreenplace.net/2012/08/13/how-statically-linked-programs-run-on-linux
@salem thank you for answering this. You've saved my life, I've wasted 4 hours trying to solve this issue.

@Repeater, thank you, will read it.
Topic archived. No new replies allowed.