MATLAB + Mex + OpenCV:正确链接和编译,但在运行时无法找到库

时间:2013-07-15 05:35:18

标签: c++ matlab opencv mex

我有一个mex函数,它使用我正在尝试使用的OpenCV。编译似乎有效,但当我尝试在MATLAB内调用该函数时,我收到此错误:

Invalid MEX-file '/path/to/project/mexfunction.mexa64': libopencv_legacy.so.2.4: cannot open shared object file: No such file or directory

我的OpenCV 2.4.5安装位于/nwdata/username/(我使用其文档中的OpenCV推荐设置从头开始编译)。我使用这个函数编译mex:

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

编译时,我没有错误。然后,我检查了库的依赖关系是使用ldd mexfunction.mexa64的。这是相关的一行:

libopencv_legacy.so.2.4 => /nwdata/username/lib/libopencv_legacy.so.2.4 (0x00002ad6a2123000)

好的,所以这似乎没问题。该文件肯定存在:

[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

在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.

这看起来很奇怪。有任何想法吗?这台机器是集群的一部分,所以我没有root权限或任何东西。

2 个答案:

答案 0 :(得分:1)

我几天前遇到过类似的问题。只有在群集上远程使用matlab时才会出现此问题。在Matlab中编辑LD_LIBRARY_FILE是没用的,我不知道为什么。我通过在.bashrc(或.bash_profile)中定义LD_LIBRARY_PATH来解决问题。

答案 1 :(得分:0)

我有一个非常类似的问题并通过在Matlab二进制文件目录中添加一个软链接来解决它,在我的情况下,所有其他库都位于其中:

sudo ln -s /users/marc/lib/libName.so /opt/matlab/MATLAB_R2014b_Linux_x86-64/bin/glnxa64/libName.so

解决了这个问题。