0

I am trying to build a MATLAB MEX file in Xcode (version 4.6.3) for debugging purposes. I was already able to successfully compile the program in MATLAB (R2013a) using this command:

>> mex src/main.c src/binomial.c src/gaussian1.c src/gaussianU.c src/poisson.c -Iinclude -I/usr/local/include -L/usr/local/lib -lmpfr -output cpdetect_c

When I try to build it in Xcode, the program compiles successfully, but the build fails at the linking stage with errors like this:

Undefined symbols for architecture x86_64:
  "_mexErrMsgIdAndTxt", referenced from:
      _mexFunction in main.o
  "_mexPrintf", referenced from:
      _find_gaussian1_change_point in gaussian1.o
      _cpdetect in main.o
      _find_poisson_change_point in poisson.o
  "_mpfr_add", referenced from:
      _find_gaussian1_change_point in gaussian1.o
      _find_gaussianU_change_point in gaussianU.o
      _find_poisson_change_point in poisson.o
  "_mxCreateDoubleMatrix_730", referenced from:
      _mexFunction in main.o

As described in this post, I have the following settings in Xcode:

>> Other Linker Flags: -lmx -lmex -lmat -lmpfr
>> Header Search Paths: $(MATLABROOT)/extern/include /Users/myName/Documents/MATLAB/SingleMolecule/Current_Version/cpdetect_c_mex/include /usr/local/include
>> Library Search Paths: $(MATLABROOT)/bin/maci64 /usr/local/lib

but this is apparently not sufficient for successful linking. What am I missing?
Thanks for any suggestions!

Community
  • 1
  • 1
dannyhmg
  • 135
  • 8

1 Answers1

0

After some more research and tinkering, I found the solution. In addition to setting the Linker Flags, Header Search Paths and Library Search Paths (under Build Settings), as described in my initial post, it's necessary to tell Xcode (a second time) to link against the libraries, as follows:

1) Select the "target" and go to "Build Phases."

2) If you don't see a "Link Binary with Libraries" item, go to Editor>Add Build Phase menu and choose "Add Link Binary with Libraries Build Phase."

3) Click the plus sign and choose the libraries you need to link against. File names have the prefix lib and the extension .dylib; for example, the mpfr library is libmpfr.dylib.

Hopefully this helps someone!

dannyhmg
  • 135
  • 8