问题
I have a mex
function which uses OpenCV
that I'm trying to use. The compilation seems to work, but when I try and call the function within MATLAB
I get this error:
Invalid MEX-file '/path/to/project/mexfunction.mexa64': libopencv_legacy.so.2.4: cannot open shared object file: No such file or directory
My OpenCV 2.4.5
install is located at /nwdata/username/
(I compiled myself from scratch using the OpenCV recommended settings from their documentation). I compile with mex using this function:
function cvmex(mexfile)
OCV_INC_DIR='/nwdata/username/include/opencv';·
OCV2_INC_DIR='/nwdata/username/include';·
OCV_LIB_DIR ='/nwdata/username/lib';·
mex(mexfile,'-g','-v',['-I',OCV2_INC_DIR],['-I',OCV_INC_DIR],['-L',OCV_LIB_DIR],'DUSE_DOUBLE',...
'-lopencv_legacy','-lopencv_imgproc','-lopencv_core','-lopencv_contrib','-lopencv_ml',...
'-lopencv_objdetect','-lopencv_calib3d','-lopencv_flann','-lopencv_features2d',...
'-lopencv_video','-lopencv_gpu');
end
When I compile, I get no errors. I then checked to see what the dependencies of the library are using ldd mexfunction.mexa64
. Here is the relevant line:
libopencv_legacy.so.2.4 => /nwdata/username/lib/libopencv_legacy.so.2.4 (0x00002ad6a2123000)
Ok, so that seems alright. That file definitely exists:
[username@machine:/nwdata/username/lib]
$ ls -l libopencv_legacy*
lrwxrwxrwx 1 username REDACTED 25 Jul 15 15:07 libopencv_legacy.so -> libopencv_legacy.so.2.4.5
lrwxrwxrwx 1 username REDACTED 25 Jul 15 15:07 libopencv_legacy.so.2.4 -> libopencv_legacy.so.2.4.5
-rwxr-xr-x 1 username REDACTED 1303944 Jun 17 15:37 libopencv_legacy.so.2.4.5
[username@machine:/nwdata/username/lib]
$ file libopencv_legacy.so*
libopencv_legacy.so: symbolic link to `libopencv_legacy.so.2.4.5'
libopencv_legacy.so.2.4: symbolic link to `libopencv_legacy.so.2.4.5'
libopencv_legacy.so.2.4.5: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, not stripped
Running on Linux:
Linux machine@redacted 2.6.43.8-1.fc15.x86_64 #1 SMP Mon Jun 4 20:33:44 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
MATLAB R2011b, and g++ 4.6.3.
This seems odd. Any ideas? This machine is part of a cluster, so I don't have root access or anything.
回答1:
I encountered a similar problem some days ago. This problem only happens when you using the matlab remotely on a cluster. Editing the LD_LIBRARY_FILE in Matlab is useless, and I don't know why. I solved the problem by defining the LD_LIBRARY_PATH in .bashrc (or .bash_profile).
回答2:
I had a very similar problem and solved it by adding a soft link to the missing library in the Matlab binaries directory, where all the other libs were located, in my case:
sudo ln -s /users/marc/lib/libName.so /opt/matlab/MATLAB_R2014b_Linux_x86-64/bin/glnxa64/libName.so
solved the problem.
来源:https://stackoverflow.com/questions/17647403/matlab-mex-opencv-links-and-compiles-correctly-but-cant-find-library-at-ru