Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

Technical Solutions

How do I debug a MEX-file using Xcode on MacOS X?


Date Last Modified: Friday, June 26, 2009
Solution ID:   1-22BGKF
Product:   MATLAB
Reported in Release:   R14SP3
Platform:   Mac
Operating System:   Mac OS 10.4x (Tiger)
 

Subject:

How do I debug a MEX-file using Xcode on MacOS X?

Problem Description:

I would like to debug a MEX-file using the Xcode development environment on a Macintosh.

Solution:

This Solution is specific to the Apple Xcode debugger on Mac. For other configurations, see the Debugging section of Tech Note 1605, MEX-files Guide:
http://www.mathworks.com/support/tech-notes/1600/1605.html

 
NOTE: This solution is not officially supported by The MathWorks.

The following steps demonstrate how to debug a MEX-file in Xcode (version 2.0) for MacOS 10.4 (Tiger) and MATLAB 7.1 (R14SP3).

1. Open Xcode and select "File -> New Project..."

2. Choose to create a project of type "Dynamic Library -> BSD Dynamic Library". Name the project as you choose. In this example, the timestwo.c MEX-file included with MATLAB (located in $MATLAB/extern/examples/refbook/) will be used for demonstration, so the project is named timestwo.

$MATLAB is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB Command Prompt.

3. In the new project, highlight "Targets -> timestwo" in the Projects and Groups pane. Choose "File -> Get Info" or press Cmd-I to bring up the settings for this target. Modify the following options in the "Build" tab:

Header Search Paths: $MATLAB/extern/include
Other Linker Flags: -flat_namespace -undefined suppress
Executable Suffix: mexmac

4. Select the "Rules" tab and select the GCC 3.3 compiler.

5. Highlight the project in the Projects and Groups pane, select "File -> Add Existing File...", and add the file $MATLAB/extern/src/mexversions.c to the list of project source files.

6. Add your C MEX-function file to the project. The timestwo.c file can be copied or added directly to the project.

7. At this point, you should be able to build your MEX-function. Press the build button and verify that the build is successful. Now you can use the MEX-function from within MATLAB.

8. To debug from within Xcode, you will need to create a custom executable that launches MATLAB from Xcode. Select "Project -> New Custom Executable".

9. Change the name of the new executable as desired, and set the location as:

$MATLAB/bin/mac/MATLAB
Please note that if you use the "Browse..." button and navigate to this directory, you will see two items named MATLAB; the one you should select is listed as a "Unix Executable".

10. Select the Arguments tab and set the following argument:

-display :0.0
You can also set any other desired MATLAB command-line options as arguments. Also set the following environment variables:

MATLAB: MATLABROOT
AUTOMOUNT_MAP: private ->
ARCH: mac
XKEYSYMDB: MATLABROOT/X11/app-defaults/XKeysymDB
XAPPLRESDIR: MATLABROOT/X11/app-defaults
MATLABPATH: MATLABROOT/toolbox/local
DYLD_LIBRARY_PATH: MATLABROOT/sys/os/mac:MATLABROOT/bin/mac:MATLABROOT/extern/lib/mac
TOOLBOX: MATLABROOT/toolbox

11. You should now be able to launch MATLAB by selecting "Debug -> Debug Executable".

12. Once MATLAB has launched, navigate to the directory where your build products are located (this may already be the current directory, depending on the settings you chose when creating the executable in Xcode). Add a breakpoint in your MEX-file and then run the MEX-function from the MATLAB prompt. It should stop on the breakpoint. You can now debug your MEX-function in Xcode as you would any other Xcode project.

 

Related Solutions:

Please provide feedback to help us improve this Solution
Contact support
E-mail this page
Print this page