Mex error for use in MATLAB on macOS

Hi,

I am using acados with MATLAB 2022 on macOS 13 with XCode Version 14.0. Libs exist in the folder <acados_root_folder>/lib.

Also, I can run the example minimal_example_ocp.py from the Terminal, as mentioned in the following issue:

However, running the same example in MATLAB gives the following error:

Warning: Name is nonexistent or not a directory: /Users/../acados/../pendulum_on_cart_model 
> In path (line 109)
In addpath (line 86)
In minimal_example_ocp (line 38) 
--------------------------------------------------------------
Structure detection for path cost
Cost function is quadratic -> Reformulating as linear_ls cost.


reformulated cost term in linear least squares form with:
cost = 0.5 * || Vx * x + Vu * u + Vz * z - y_ref ||_W

Vx
     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1
     0     0     0     0


Vu
     0
     0
     0
     0
     1


Vz

W
   1.0e+03 *

    2.0000         0         0         0         0
         0    2.0000         0         0         0
         0         0    0.0000         0         0
         0         0         0    0.0000         0
         0         0         0         0    0.0000


y_ref
     0
     0
     0
     0
     0


y (symbolic)
[p, theta, v, dtheta, F]

NOTE: These numerical values can be updated online using the appropriate setters.
--------------------------------------------------------------
--------------------------------------------------------------
Structure detection for terminal cost term
Cost function is quadratic -> Reformulating as linear_ls cost.


reformulated cost term in linear least squares form with:
cost = 0.5 * || Vx * x + Vu * u + Vz * z - y_ref ||_W

Vx
     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


Vu
     0
     0
     0
     0


Vz

W
   1.0e+03 *

    2.0000         0         0         0
         0    2.0000         0         0
         0         0    0.0000         0
         0         0         0    0.0000


y_ref
     0
     0
     0
     0


y (symbolic)
[p, theta, v, dtheta]

NOTE: These numerical values can be updated online using the appropriate setters.
--------------------------------------------------------------

Constraint detection for path constraints.
constraint 1 is reformulated as bound on u.
F
 
found compiled acados MEX interface
Error using acados_ocp
Exception:
	MATLAB:mex:ErrInvalidMEXFile
	Invalid MEX-file '/Users/../acados/build/ocp_create.mexmaci64':
    dlopen(/Users/../acados/build/ocp_create.mexmaci64, 0x0006): Library not loaded:
    @rpath/libacados.dylib
  Referenced from: <AA8D9CF0-EE3B-3C72-A79D-6175B9B75F0F>
  /Users/../acados/build/ocp_create.mexmaci64
  Reason: tried: '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/./libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/../../sys/os/maci64/libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/./libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/../../sys/os/maci64/libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/../../runtime/maci64/libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/../../sys/java/jre/maci64/jre/lib/server/libacados.dylib' (no
  such file), '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/../../sys/java/jre/maci64/jre/lib/libacados.dylib'
  (no such file), '/System/Volumes/Preboot/Cryptexes/OS@rpath/libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/./libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/../../sys/os/maci64/libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/./libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/../../sys/os/maci64/libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/../../runtime/maci64/libacados.dylib' (no such file),
  '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/../../sys/java/jre/maci64/jre/lib/server/libacados.dylib' (no
  such file), '/Applications/Computation/MATLAB_R2022b.app/bin/maci64/../../sys/java/jre/maci64/jre/lib/libacados.dylib'
  (no such file), '/usr/local/lib/libacados.dylib' (no such file), '/usr/lib/libacados.dylib' (no such file, not in dyld
  cache)


Error in minimal_example_ocp (line 108)
ocp = acados_ocp(ocp_model, ocp_opts);

Hi,

it seems like Matlab is not looking in the correct folder for the compiled acados lib.
Did you export the paths properly.
Maybe you have to additionally tell Mac that it should look for libs there.
Alternatively, you could install acados into /usr/local/lib.

Hi,

As mentioned in the following answer, acados path is already exported.

However, I notice that in the Python example mentioned there, the variable ACADOS_SOURCE_DIR is used to export the path, as in export ACADOS_SOURCE_DIR="/Users/.../acados". In the MATLAB example, the variable ACADOS_INSTALL_DIR is used. Nevertheless, I add the following in my .zshrc file: export ACADOS_INSTALL_DIR="/Users/.../acados".

Another thing is that after I execute source env.sh in the terminal, I get the following:

ACADOS_INSTALL_DIR=/Users/.../acados/examples/acados_matlab_octave/getting_started/../../..

This is not the actual acados installation path, but the example path. I am not certain if this is the desired behavior.

Also, the following is given when executing source env.sh:

LD_LIBRARY_PATH=:/Users/.../acados/examples/acados_matlab_octave/getting_started/../../../lib:./build:/Users/.../acados/examples/acados_matlab_octave/getting_started/../../../interfaces/acados_template/tera_renderer/t_renderer/target/release:/Users/.../acados/examples/acados_matlab_octave/getting_started/../../../lib:./build:/Users/.../acados/examples/acados_matlab_octave/getting_started/../../../interfaces/acados_template/tera_renderer/t_renderer/target/release

As can be seen, the variable LD_LIBRARY_PATH is used, instead of DYLD_LIBRARY_PATH. Is this also as expected?

Your path"/Users/.../acados" is a bit confusing to me.
What do the 3 dots mean?

If you don’t move the example around, this should give you the correct ACADOS_INSTALL_DIR, i.e. 3 folders up from the example.

I am not sure how it was with DYLD_LIBRARY_PATH in Matlab.
Maybe also our Mac expert @aghezz1 can have a look.

I replaced some characters in the complete path with these three dots, so the actual path is something like /Users/x/acados.

If you don’t move the example around, this should give you the correct ACADOS_INSTALL_DIR , i.e. 3 folders up from the example.

I did not move the example file around or any files within the root acados folder.

Hi fran,

I can reproduce your error if I comment out DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:"/Users/acados/lib" from my .zshrc profile.

You are right about the env.sh file, it is sourcing the LD_LIBRARY_PATH which is for linux system, but this shouldn’t be a problem if you have included the acados/lib in the DYLD_LIBRARY_PATH as reported in the Installation guide.

To sum up, if you have acados/lib in the DYLD_LIBRARY_PATH then everything should work.
One thing that you have to check if that you can edit the DYLD_LIBRARY_PATH. In fact, macOS doesn’t allow the user to edit it by default, check this post.

You can check what Matlab exports by executing ! export -p in Matlab Command Window and see if acados is there.

Let me know if you can solve the issue!

Good luck,
Andrea

1 Like

Hi Andrea,

Thanks for the help!

The path from ! export -p does not include acados. This might indicate that the acados path cannot be found by MATLAB, I guess.

I will need to think about whether I would like to use the SIP solution as you suggested.

Hi Fran,

If you don’t want to disable SIP but use acados, you can put the dynamic libraries inside acados/lib in the default path /usr/local/lib.
Then you can add /usr/local/lib to Matlab path, by using addpath.

I think the all post is useful for your case :stuck_out_tongue:

Cheers,
Andrea

Hi Andrea,

Thank you for the help! Your solution is working for me now!

I see the following warning, though. However, this is relevant to the current issue.

ld: warning: dylib (/Users/acados/examples/acados_matlab_octave/getting_started/build/libpendulum.so) was built for newer macOS version (12.3) than being linked (10.15)
ld: warning: ld: warning: dylib (/Users/acados/lib/libqpOASES_e.dylib) was built for newer macOS version (12.3) than being linked (10.15)
1 Like