MATLAB: How to build an Engine application using the Xcode IDE on Mac

applicationcocoaengineidemacMATLABxcode

I would like to build an Engine application such as ENGDEMO.C on a Mac using the Xcode IDE instead of the MEX script in MATLAB.

Best Answer

These instructions are written for Engine applications on Intel Macs running 32-bit MATLAB. For PowerPC Macs, substitute 'mac' for 'maci' in each of the paths listed below, while for 64-bit Intel Macs, substitute 'maci64' for 'maci'.
First, ensure that you are using a supported version of Xcode and gcc for your version of MATLAB:
1. Get the Unix shell PATH variable. Add ".", "$MATLABROOT/bin" and "$MATLABROOT/bin/maci".
where $MATLABROOT is your MATLAB installation directory, as returned by executing:
matlabroot
at the MATLAB Command Prompt.
2. Create new project, Command Line Utility: Standard Tool, with project name 'projname'. Select C/C++ Style not the default Foundation.
3. Add engdemo.c as main file.
4. Project -> Edit Project Settings, choose Build tab, select Search Paths group.
5. Select Architecture as x86 (32 bit Universal).
6. Header Search Path: Add
$MATLABROOT/extern/include
This search path setting corresponds to the "-I$MATLABROOT/extern/include" flags passed to gcc.
7. Select "Library search path" and click Edit. Click + and add "$MATLABROOT/bin/maci" and "$MATLABROOT/runtime/maci". Leave the existing "$(inherited)" item.
This setting corresponds to the "-L$MATLABROOT/bin/maci" flag passed to gcc.
8. Select the Preprocessing section, and set Preprocessor Macros to "MATLAB_MEX_FILE MX_COMPAT_32 NDEBUG".
This corresponds to the "-DMATLAB_MEX_FILE -DMX_COMPAT_32 -DNDEBUG" flags passed to gcc.
9. Switch to "Code Generation" group and select "No Common Blocks".
This corresponds to "-fno-common".
10. Set Optimization Level to "Fastest [-O3]".
This corresponds to "-O3".
11. Select "Language" group and ensure that "Enable C++ Exceptions" is checked.
This corresponds to "-fexceptions".
12. Switch to "Linking" group. Set "Other Linker Flags" to "-Wl,-flat_namespace -undefined suppress, -lmx and -leng".
13. In Project window, select Targets -> projname -> Link Binary with Libraries, control-click and select Add, and pick these libraries:
$MATLABROOT/bin/maci/libeng.dylib
$MATLABROOT/bin/maci/libmx.dylib
Ensure that "Copy files..." is not checked. This corresponds to the "-leng -lmx" flags passed to gcc.
14. Select Project->Edit Active Executable 'projname'. Switch to Arguments tab.
15. Add Unix shell PATH variable with value from step 1.
16. Add Unix shell DYLD_LIBRARY_PATH variable with the following values:
$MATLABROOT/runtime/maci:
$MATLABROOT/bin/maci:
$MATLABROOT/sys/os/maci:
/System/Library/Frameworks/JavaVM.framework/JavaVM:
/System/Library/Frameworks/JavaVM.framework/Libraries
Also, add variable XAPPLRESDIR with value:
$MATLABROOT/X11/app-defaults
This corresponds to the environment variables needed for Engine applications.
17. In Source window, click Build.
18. In Source window, click Build & Run. You should now be executing an Engine application within Xcode.