Error while compiling/linking OpenCV Matlab source

2019-06-09 04:30发布

问题:

After successfully compiling 95% of OpenCV, I get the error

(...)
[ 95%] Built target opencv_videostab
[ 95%] Generating Matlab source files
[ 95%] Built target opencv_matlab_sources
[ 95%] Compiling Matlab source files. This could take a while...
CMake Error at compile.cmake:47 (message):
  Failed to compile logPolar: /usr/bin/ld: cannot find -lopencv_core

  /usr/bin/ld: cannot find -lopencv_imgproc

  /usr/bin/ld: cannot find -lopencv_ml

  /usr/bin/ld: cannot find -lopencv_highgui

  /usr/bin/ld: cannot find -lopencv_objdetect

  /usr/bin/ld: cannot find -lopencv_flann

  /usr/bin/ld: cannot find -lopencv_features2d

  /usr/bin/ld: cannot find -lopencv_photo

  /usr/bin/ld: cannot find -lopencv_video

  /usr/bin/ld: cannot find -lopencv_videostab

  /usr/bin/ld: cannot find -lopencv_calib3d

  /usr/bin/ld: cannot find -lopencv_stitching

  /usr/bin/ld: cannot find -lopencv_superres

  /usr/bin/ld: cannot find -lopencv_nonfree

  collect2: ld returned 1 exit status



      mex: link of ' "logPolar.mexa64"' failed.





make[2]: *** [modules/matlab/compile.proxy] Error 1
make[1]: *** [modules/matlab/CMakeFiles/opencv_matlab.dir/all] Error 2
make: *** [all] Error 2

This confuses me, because as far as I understand all these should have been compiled earlier in the build process. The cmake command used was

cmake -D MATLAB_ROOT_DIR=/usr/lib/matlab2012a/ -D CMAKE_INSTALL_PREFIX=/home/user ..

回答1:

I had the same problem, I guess there's a reason why it's not yet included in the official distribution. My solution was the following:

  1. Compile OpenCV without the Matlab module, for me this ẃas simply not specifying where Matlab was located since it wasn't in a standard location, but it should be possible by setting a variable in cmake or modifying the CMake configuration files.
  2. Install OpenCV (make install), this will install the necessary libraries.
  3. Run cmake again, this time including the Matlab option, run make.
  4. At this point, a bunch of errors occur while compiling some of the generated Mex source files. I simply removed the files that would not compile. These are the files I had to remove:
createAlignMTB.cpp
createCalibrateDebevec.cpp
createCalibrateRobertson.cpp
createLineSegmentDetector.cpp
createMergeDebevec.cpp
createMergeMertens.cpp
createMergeRobertson.cpp
createTonemap.cpp
createTonemapDrago.cpp
createTonemapDurand.cpp
createTonemapMantiuk.cpp
createTonemapReinhard.cpp
findCirclesGrid.cpp

I did not need any of them, hopefully the same is true for you, if you do you'll need to resolve the errors. (the source files don't seem to be regenerated once generated so may be safely modified)

  1. make install!
  2. ???
  3. Profit!


回答2:

When encountering an error with building something with cmake, it's always a good idea to rerun it with VERBOSE=1 option. 

I had a similar issue with '/usr/bin/ld: cannot find ....' and make VERBOSE=1 revealed that the the linker path to opencv libraries is incorrectly set to L${ROOT_TO_OPENCV_BUILD}/lib/RelWithDebInfo whereas it should be L${ROOT_TO_OPENCV_BUILD}/lib. So the problem is somewhere in the CMake config files.

A super dirty workaround is to simply create a fake link like this cd ${ROOT_TO_OPENCV_BUILD}/lib; ln -s . RelWithDebInfo This will make the compilation continue.fails for me later on createTonemapReinhard.cpp error: ‘Ptr_TonemapReinhard’ was not declared in this scope Clearly the matlab mex module is not yet ready.