Tutorial for libmat under XCode 4.2

조회 수: 2 (최근 30일)
Jeff Mandel
Jeff Mandel 2012년 3월 4일
편집: uvcatastrophe 2016년 4월 21일
I spent my day today trying to get the example "diagnose" to compile under Xcode 4.2.1 on OS X Lion with Matlab 2011b. I deduced that I needed to add linker options to include libmat and libmex (-lmat -lmx), and add the library search paths for bin/maci64 and runtime/maci64, but even when I get Xcode to compile and link, it ultimate fails the run, usually failing to find libtbb. I looked at mcc, and worked through all of the changes to mbuildopts.sh required to get Matlab to successfully compile, but again, it fails at execution, looking for libraries. Yes, I RTFM, and googled, and still no luck.
Could someone put together a simple tutorial on how to compile a C program that uses libmat and libmx under Xcode 4.2?
Thanks

답변 (1개)

uvcatastrophe
uvcatastrophe 2016년 4월 21일
편집: uvcatastrophe 2016년 4월 21일
You're right, there's not much info out there about this. I ran a similar problem, and I believe to have solved it, but not without first bricking my Mactop!
FYI I did this in Xcode 7.2. For most linking against most libraries, it suffices to take the following steps:
  • Specify search paths for the header and library files. In Targets > Build Settings > Search Paths > Header/Library Search Paths and add ${MATLABHOME}/extern/include to the Header Search Path field and ${MATLABHOME}/bin/maci64 to the Library Search Path field.
  • Add the library files themselves. Go to Build Phases (next to Build Settings) and add the two libraries, libmat.dylib and libmx.dylib, under the subheading Link Binary With Libraries. You shouldn't need to add -lmat or -lmx flags -- Xcode takes care of this.
  • In some cases (when the library is dynamically linked and loaded) you will have to pass an additional rpath flag. In Xcode, go back to Build Settings > Linking > Runpath Search Paths and add the directory above where the dylibs live, ${MATLABHOME} to this field.
On the command line this last step would look like
clang++ ${SRC} -I${MATLABHOME}/extern/include -L${MATLABHOME}/bin/maci64 \
-lmat -lmx -Wl,-rpath,${MATLABHOME}/bin/maci64
versus
clang++ ${SRC} -I${MATLABHOME}/extern/include -L${MATLABHOME}/bin/maci64 -lmat -lmx
without the last step. Insanely, you specify a comma after the rpath (-rpath,${LIB}) flag, not an equals sign (-rpath=${LIB}) like you would on a Linux machine.
This should just about do it, except there's one more caveat that took me several hours to figure out, and I only figured it out after it was too late. Someone had mentioned they got this to work elsewhere on the Internet only after copying libmat and libmx to the /usr/lib folder. DO NOT DO THIS ON A MAC!! It turns out there is another dylib in /usr/lib by the name of libmat.dylib (!!!). I had to clean install the entire OS (thankfully, all my files were saved) because it broke linkage with libSystem.B.dylib. After I got everything working again, I realized that I had to put the Matlab search path BEFORE /usr/lib in the search paths, because otherwise it would find the other libmat.dylib and try to link with that instead.
I am really hoping this helps, and that someone else can avoid making the same mistake I did. And if anyone at MATLAB is reading this, I just wanted you to know that you named your dynamic library the same name as a system dylib on OS X. Good luck!

카테고리

Help CenterFile Exchange에서 C Shared Library Integration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by