Compiling MATLAB engine demo on Mac OS X with gcc

Alec Jacobson

March 12, 2010

weblog/

I followed the MATLAB instructions on compiling a C program that uses the matlab engine to call matlab functions. The works find when I run the commands from MATLAB (with the exception that I must use !./engdemo with the ./ to make the thing run). But if I try to compile the program on the command line using gcc, I get this error:
Undefined symbols:
  "_mxCreateDoubleMatrix_730", referenced from:
      _main in ccpVU28R.o
  "_engOutputBuffer", referenced from:
...
ld: symbol(s) not found
collect2: ld returned 1 exit status
After much searching around I have found a solution to this problem. Not sure how good it is since it involves messing with DYLD_LIBRARY_PATH and I've been warned never to mess with that. None the less here's what I did: First in your ~/.profile add (or just type into the shell):
export MATLAB="/Applications/MATLAB_R2009aSV.app/"
You may have to change MATLAB_R2009aSV.app to the name of the MATLAB program folder on your computer. Then in the shell (you will have to do this everytime you compile or add this to your ~/.profile):
export DYLD_LIBRARY_PATH=$MATLAB/bin/maci/:$MATLAB/sys/os/maci/:DYLD_LIBRARY_PATH
64-bitters may need to change the maci to maci-64 or something. Finally compile using all these flags and parameters:
g++ -o engdemo engdemo.cpp -I$MATLAB/extern/include/ -L$MATLAB/bin/maci/ -leng -lm -lmat -lmx -lut 
or
gcc -o engdemo engdemo.c -I$MATLAB/extern/include/ -L$MATLAB/bin/maci/ -leng -lm -lmat -lmx -lut 
A similar solution for linux Update: If you run into an error that goes something like this:
ImportError: dlopen([your library], 2): Library not loaded: @loader_path/libeng.dylib
Then you need to change your DYLD_LIBRARY_PATH as mentioned above. Although then in a different library I get a new error:
ImportError: dlopen(lib/_Drawers.so, 2): Library not loaded: /opt/local/lib/libmpfr.1.dylib
  Referenced from: [your other library]
  Reason: Incompatible library version:  [your other library] requires version 4.0.0 or later, but libmpfr.1.dylib provides version 3.0.0
Which seems to be because MATLAB has its own out of date libmpfr.1.dylib which causes CGAL to belch. Haven't come upon a solution for this yet but will post if I do.