MATLAB Answers

2

How do *you* debug mex files on Mac OS

Asked by Oliver Woodford on 22 Oct 2015
Latest activity Answered by Oliver Woodford on 15 Nov 2016
I'm having trouble debugging on a Mac for the first time. I'd like to know how you do it.

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by Rahul Goel on 26 Oct 2015

Hi Oliver, you can follow the steps below to debug your MEX-file using Xcode 4.3, Xcode 4.4 or Xcode 4.5 on OS X 10.7 (Lion) or OS X 10.8 (Mountain Lion) :
1. Download Templates.zip (the Xcode MEX-Library project template) and put it into: Applications/<Xcode>/Developer/Library/Xcode where Xcode is the folder for the specific version of Xcode you are using.
make sure you follow the folder structure as shown below.
You do not need to quit Xcode for it to recognize the template.
2. Open Xcode and select "File --> New --> Project..."
3. On the left column, choose the category MathWorks and select type MEX-Library.
4. Name the "Product Name" after your MEX-function (e.g. timestable, the example located in $MATLAB/extern/examples/refbook).
5. Enter the name of your MATLAB application as it appears in finder, e.g. MATLAB_R2012b.
6. Add your MEX-function file(s) into the project hierarchy (e.g., drag and drop), making sure to check 'Copy items into destination group's folder' so that the file(s) will be copied into your project directory and also make sure the target checkbox is checked.
7. In XCode select your project and go to its "Build Settings". Make sure that you are in the "All" view (instead of "Basic") and then look for the "Optimization Level" setting; this setting should be set to "None". If the Optimization Level is set to anything else than None, it is not guaranteed that your code is executed in the order that it has been written which would make efficient debugging virtually impossible.
8. At this point, everything has been setup for you, so hit Cmd-B to build your MEX-function and verify that the build is successful. Now you can use the MEX-function from within MATLAB.
9. To debug from within Xcode, launch MATLAB as you normally would and attach to the MATLAB process from within Xcode.
10. In MATLAB, cd to the place where the Xcode product was built, i.e.
cd $(YOUR_XCODE_PROJECT_DIR)/build/Debug
(the product is placed in the subdirectory build/$(CONFIGURATION) of the project root).
11. Add a breakpoint in your MEX-file and then run the MEX-function from the MATLAB prompt. The control should stop at the break-point in Xcode.
Hope this helps.

  1 Comment

Thanks, Rahul. Where would I find Templates.zip?

Sign in to comment.


Answer by Oliver Woodford on 15 Nov 2016

This is how I do it.

  0 Comments

Sign in to comment.