DIPlib / diplib

Quantitative Image Analysis in C++, MATLAB and Python
https://diplib.org
Apache License 2.0
222 stars 49 forks source link

Invalid MEX-file measure.mexmaca64 #144

Closed ronligt closed 6 months ago

ronligt commented 8 months ago

Component DIPimage Git commit ID: c81e4933e9495044dca4fa85b786ac1c57fbac59

Describe the bug Error-message in Matlab R2023b (maca64) after calling dipimage:

Invalid MEX-file '/Users/rligteringen/dip/share/DIPimage/private/numberofthreads.mexmaca64':
dlopen(/Users/rligteringen/dip/share/DIPimage/private/numberofthreads.mexmaca64, 0x0006):
Library not loaded: @rpath/libMatlabEngine.dylib
  Referenced from: <89B83B3E-18E8-3F81-9210-A8AFB6FAFF22>
  /Users/rligteringen/dip/share/DIPimage/private/numberofthreads.mexmaca64
  Reason: tried:
  '/Users/rligteringen/dip/share/DIPimage/private/../../../lib/libMatlabEngine.dylib' (no such
  file), '/Users/rligteringen/dip/share/DIPimage/private/../../../lib/libMatlabEngine.dylib'
  (no such file), '/Applications/MATLAB_R2023b.app/bin/maca64/libMatlabEngine.dylib' (no such
  file), '/Applications/MATLAB_R2023b.app/bin/maca64/./libMatlabEngine.dylib' (no such file),
  '/Applications/MATLAB_R2023b.app/bin/maca64/../../sys/os/maca64/libMatlabEngine.dylib' (no
  such file), '/Applications/MATLAB_R2023b.app/Contents/MacOS/libMatlabEngine.dylib' (no such
  file), '/Applications/MATLAB_R2023b.app/Contents/MacOS/./libMatlabEngine.dylib' (no such
  file),
  '/Applications/MATLAB_R2023b.app/Contents/MacOS/../../standalone/bin/maca64/libMatlabEngine.dylib'
  (no such file),
  '/Applications/MATLAB_R2023b.app/Contents/MacOS/../../sys/os/maca64/libMatlabEngine.dylib'
  (no such file), '/Applications/MATLAB_R2023b.app/bin/maca64/libMatlabEngine.dylib' (no such
  file)

To Reproduce

  1. Build DIPimage with instructions https://diplib.org/diplib-docs/building_macos.html on M2 with Matlab 2023b for Apple Silicon chip.
  2. Start Matlab
  3. addpath
  4. dipimage

System information:

More information I've checked the output of otool -L in Matlab:

>> !otool -L numberofthreads.mexmaca64
numberofthreads.mexmaca64:
    @rpath/numberofthreads.mexmaca64 (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libMatlabEngine.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libMatlabDataArray.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libmex.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
    @rpath/libDIP.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1600.157.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)
crisluengo commented 8 months ago

Those instructions haven't yet been updated to account for the new Apple Silicon release of MATLAB. If you have the latest version of MATLAB, you likely are running it natively and not through Rosetta. So you need to build an Apple Silicon version of DIPimage, not an Intel version.

crisluengo commented 8 months ago

I've updated the instructions to distinguish aarch64 and x86_64 versions of MATLAB: https://github.com/DIPlib/diplib/commit/c2bf27464eef14c6337ca7cb4acd10425057dd8a

ronligt commented 7 months ago

Thanks for updating the instructions!

The problem however does not relate to the Apple Silicon compilation of DIPimage. As it turns out with the latest version of FindMatlab.cmake the same error in Matlab is generated on an Intel compilation, both on an Intel Macintosh and a M2 Macintosh with Rosetta emulation.

A quick solution for Intel compilation is to check out the previous version of FindMatlab.cmake (https://github.com/DIPlib/diplib/commit/376fb4fd10b9b0a969a1a042fc222e6f99a87e8b) and build DIPimage. This however does not work for native Apple Silicon compilation.

It looks like there is a problem related to libMatlabEngine.dylib and/or libMatlabDataArray.dylib in the new version of FindMatlab.cmake but I find it very hard to debug this in the cmake-file.

crisluengo commented 7 months ago

The older cmake script doesn’t work correctly with the newest MATLAB. On Intel it’s likely finding an older version.

Can you show me the output of the cmake command, with the latest version of the scripts, on aarch64?

I have your same version of MATLAB, and am able to build just fine for it.

crisluengo commented 6 months ago

The change to tools/FindMatlab.cmake hidden in (totally unrelated commit) 68f4d8fc fixes this issue.

I should have been more careful in making that change as a separate commit. :(