MATLAB Answers

Can I build a C++ MEX file using Xcode 5 on Mac OS X 10.9?

13 views (last 30 days)
Daniel Golden
Daniel Golden on 1 Nov 2013
Commented: Mike Damianakis on 5 Jun 2014
I would like to be able to build MEX files using the Xcode 5 IDE on OS X 10.9 (Mavericks) with Matlab 2013b.
I saw this related question involving what was presumably Xcode 4 on an earlier version of OS X and Matlab, but some of the steps seem different or not applicable.
I initially attempted to build the built-in mexcpp.cpp file using Xcode (on my computer, mexcpp.cpp is /Applications/MATLAB_R2013b.app/extern/examples/mex/mexcpp.cpp). Here's what I did:
  • Create a new Xcode project (OS X -> Application -> Command Line Tool)
  • Call it "mex xcode test", type C++
  • Rename main.cpp to mexcpp.cpp
  • Copy contents of matlab's mexcpp.cpp to my mexcpp.cpp
  • Under Target -> Build Settings, add the following User Header Search Paths:
  1. /Applications/MATLAB_R2013b.app/extern/include (non-recursive)
  • Under Target -> Build Settings, add the following Library Search Paths:
  1. /Applications/MATLAB_R2013b.app/sys/os/maci64 (non-recursive)
  2. /Applications/MATLAB_R2013b.app/bin/maci64 (recursive)
  • Under Target -> Build Settings, set "Always Search User Paths" to Yes
  • Build via Product -> Build
Ignoring the "missing main.cpp" warning, I get the following linker errors:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
"_mexErrMsgIdAndTxt", referenced from:
_mexFunction in mexcpp.o
"_mxGetPr", referenced from:
_mexFunction in mexcpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • Under Target -> Build Settings, add preprocessor macro MATLAB_MEX_FILE to both Debug and Release phases
This made no difference.
I thought of trying a few things from the answer to this related question from 2011
  • Under Target -> Build Phases -> Link Binary with Libraries, I added several explicit libraries (but shouldn't these be found automatically based on my library search path?):
  1. /Applications/MATLAB_R2013b.app/bin/maci64/libeng.dylib
  2. /Applications/MATLAB_R2013b.app/bin/maci64/libmx.dylib
This solved my linker problem to mxGetPr, so my linker error message is now shorter:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
"_mexErrMsgIdAndTxt", referenced from:
_mexFunction in mexcpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Is the solution then to somehow figure out which explicit library files I need for every C++ function that I write? This seems unsustainable, since there are over 500 library files in the /Applications/MATLAB_R2013b.app/bin/maci64 folder. Or am I missing a step in telling Xcode where to find the library files?
And will adding the right library files solve my error involving the _main symbol? Or do I need to somehow tell Xcode to build a library instead of an executable?
What steps am I missing?
Thank you!
  1 Comment
David
David on 1 Apr 2014
Hi, did you ever solve this problem? I am having the same issues right now. Also, in Xcode 5.1 you can't type in the explicit path to libraries like you did above.

Sign in to comment.

Answers (1)

Bernard
Bernard on 29 Apr 2014
Hi
I could make it work by setting Mach-O Type to Bundle (in Linking). Also, I used "-lmx -lmex -lmat" in Other Linker Flags to specify the libraries.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!