Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Troubleshooting (UNIX)

Amro edited this page Nov 18, 2017 · 6 revisions

Troubleshooting (UNIX)

Invalid MEX file or Segmentation fault

If MATLAB says 'Library not loaded' or any other similar error in the tests, it's likely a compatibility issue between a system library and an internal MATLAB library. You might be able to fix this issue by preloading the library file.

On Linux, set the correct library path in LD_PRELOAD environment variable. For example, if you see GLIBCXX_3.4.15 error in MEX, use the following to start MATLAB.

$ LD_PRELOAD=/usr/lib/libstdc++.so.6 matlab

Note that you need to find the correct path to the shared object. For example, /usr/lib64/ instead of /usr/lib/. You can use locate command to find the location of the shared libraries.

To find what library is conflicting, use ldd command both in the Unix shell and within MATLAB to one of the compiled MEX-files. For example:

$ ldd +cv/imread.mexa64    # within UNIX shell
>> !ldd +cv/imread.mexa64  % within MATLAB

Find any differences in the loaded libraries. Such libraries are likely to be causing the conflict. Try to preload those libraries before launching MATLAB.

It is probably handy to define a Bash alias to launch MATLAB while preloading conflicting libraries. Put the following in your .bashrc for example:

alias matlab='LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6:/lib/x86_64-linux-gnu/libgcc_s.so.1 matlab'

Note that if you have the Computer Vision System Toolbox (CVST) from MathWorks, you will likely encounter this type of errors, because that toolbox internally loads its own version of OpenCV. In that case, use the LD_PRELOAD technique described above to force loading your own OpenCV installation.

macOS

On Mac, you would use otool -L command instead of ldd. The otool -L dependency check might not show the direct conflict, but one of the dependencies of the listed library could be causing an issue.

For example, libtiff shipped with MATLAB tends to cause a compatibility issue. This can only be seen by checking the second dependency. The comparison between the following shows no conflict:

  • Terminal

      $ otool -L +cv/imread.mexmaci64
    
  • MATLAB

      >> !otool -L +cv/imread.mexmaci64
    

However, the second dependency libopencv_imgcodecs.dylib could show a conflict:

  • Terminal

      $ otool -L /usr/local/opt/opencv3/lib/libopencv_imgcodecs.3.1.dylib
    
  • MATLAB

      >> !otool -L /usr/local/opt/opencv3/lib/libopencv_imgcodecs.3.1.dylib
    

On Mac OS X, the preloading environment variable is named DYLD_INSERT_LIBRARIES. Usage:

$ DYLD_INSERT_LIBRARIES=/opt/local/lib/libtiff.3.dylib /Applications/MATLAB_R2016a.app/bin/matlab

Depending on Mac and MATLAB versions, you might also need to force a flat-namespace linking mode using:

$ DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=[...] matlab

If the above preloading method doesn't work, an alternative solution is to replace any conflicting internal library inside the MATLAB directory with a symlink pointing to the system's library:

$ cd /Applications/MATLAB_R2016a.app/bin/maci64
# make a backup of the library
$ mkdir backups
$ mv libtiff.5.dylib backups/
# replace it with a symbolic link
$ ln -s /usr/local/opt/libtiff/lib/libtiff.5.dylib

OS X: Compile Error

In Mac OS X, MATLAB may require additional setup to use mex depending on the OS version. See MathWorks support for more information.

OS X 10.9 and XCode 5

Due to the change in the default C++ runtime in OS X 10.9, you probably need to tweak a few configurations in mexopts.sh to avoid compile issues. If you haven't run mex -setup in MATLAB, please do so first and edit mexopts.sh and change a few variables. Following shows an example:

CC='clang'
CXX='clang++'
SDKROOT='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/'
MACOSX_DEPLOYMENT_TARGET='10.9'
CLIBS="$CLIBS -lc++"
CXXFLAGS="-fno-common -no-cpp-precomp -fexceptions -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET -std=c++11 -stdlib=libc++ -DCHAR16_T"
CXXLIBS="$MLIBS -lc++"