0

Basically, this question is the same as this post while unsolved so far. Here is my CMake scripts:

FIND_PACKAGE(OpenCV REQUIRED)
FIND_PACKAGE(Matlab REQUIRED)

SET(Matlab_DEP_LIBS ${Matlab_LIBRARIES} libmx.so libmat.so)
SET(OpenCV_DEP_LIBS opencv_core opencv_imgproc opencv_imgcodecs)

ADD_EXECUTABLE(myApp ${mySources})
TARGET_LINK_LIBRARIES(myAPP ${Matlab_DEP_LIBS}                        
${OpenCV_DEP_LIBS})

compiler reported link errors:

/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFReadDirectory@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFWriteEncodedStrip@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFOpen@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFReadEncodedStrip@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFSetField@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFWriteScanline@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFGetField@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFScanlineSize@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFNumberOfStrips@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFSetWarningHandler@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFSetErrorHandler@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFReadEncodedTile@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFReadRGBATile@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFClose@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFRGBAImageOK@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFClientOpen@LIBTIFF_4.0'
/usr/local/lib/libopencv_imgcodecs.so.3.3.1: undefined reference to `TIFFReadRGBAStrip@LIBTIFF_4.0'

The problem is that there also exits a libtiff.so.5 in /usr/local/MATLAB/R2015b/bin/glnxa64. It seems linker messes up at this point. So instead of renaming libtiff.so.5 in Matlab's binary directory, what is the proper workaround?

Finley
  • 795
  • 1
  • 8
  • 26
  • If matlab shared libraries are linked with `libtiff.so.4` but opencv shared libraries are linked with `libtiff.so.5`, then you have no other choice than **rebuild** matlab or opencv, so both will be linked with the same `libtiff` library. With `ldd /usr/local/lib/libopencv_imgcodecs.so.3.3.1` you may check which libraries are required for `libopencv`; same approach may be used for check `matlab` libraries. – Tsyvarev Jan 11 '19 at 15:34
  • @Tsyvarev Hi! I run `ldd ...` as you said and found OpenCV linked `libtiff.so.5` but `libmat.so` ,`libmx.so` and `libmex.so` do not require `libtiff.so.5 ` or `libtiff.so.4` at all. – Finley Jan 11 '19 at 15:56
  • Hm, then it is something wrong with your libraries. Are you sure that you didn't rename them in the past? I mean, rename manually, as OS package manager never do such things (when new version of the library is released, it just adds new `lib*.so.x.y` file without removing old ones). – Tsyvarev Jan 11 '19 at 16:02
  • I ensure I did not rename before, In effect, it worked once I renamed the `libtiff.so.5` in `/usr/local/MATLAB/R2015b/bin/glnxa64`. – Finley Jan 11 '19 at 16:14

1 Answers1

0

I've noticed the same thing with MATLAB R2016a (not with later versions) and MEX-files that use LibTIFF (not through OpenCV though). I think they did something weird with the LibTIFF library in that version of MATLAB. Either that or newer MATLABs have a newer version of LibTIFF that can satisfy all the requirements of my MEX-files.

Even if you properly set the right RPATH in your MEX-file, MATLAB will already have loaded the LibTIFF shared library, so when it loads your MEX-file, it will be linked to the LibTIFF that is in memory already.

The solution, as always, is to use static linking. Rebuild OpenCV to statically link LibTIFF. If you use static linking, your code will always use the LibTIFF functions they were linked with, and never those in any shared object that happens to be in memory.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120