如何在 Mac OS X 版的 MATLAB 中使用 LIBSVM

久沒寫文章了,而且本部落格中隱藏的 MediaWiki 因為被我在升級時一個不小心給搞壞掉了。所以,原本這篇文章應該是丟在 MediaWiki 中的,但在 MediaWiki 還沒修好之前,我只好放在部落格裡面備忘,順便讓有興趣的人可以參考。

我寫這篇文章是因為要給 LIBSVM 作者附在他的 README 文件中供使用者參考的,而原本的 README 是以英文撰寫,所以我就以英文來撰寫,讓原作者可以不必再花心思從中文改寫。以下就是如何在 Mac OS X 版的 MATLAB 2015b 中編譯、安裝及使用 LIBSVM 的說明。後續如果我還有空的話,希望可以補一些圖片上來作輔助說明。

On Mac OS X, there is no pre-build binary file for use. After you got the source code, open it, and using ‘make’ command under terminal to build, you will get the message like this: ‘No supported compiler or SDK was found. For options, visit http://www.mathworks.com/support/compilers/R2015b/maci64.html’. But there is no workaround.

Following instruction will bring you a way to build libsvm binary files for MATLAB 2015b Mac OS X version.

Preparation:

1. Install Xcode from App Store first. In this instruction, we use the Xcode 7.1.1.

2. Install ‘Xcode Command Line Tools’. Download it from Apple Developer Center website.
This package will install command line developer tools, as well as OS X SDK frameworks
and headers.

3. Install MATLAB 2015b in the directory ‘/Applications’.

4. Using text editor to edit the two files:

/Applications/MATLAB_R2015b.app/bin/maci64/mexopts/clang_maci64.xml
/Applications/MATLAB_R2015b.app/bin/maci64/mexopts/clang++_maci64.xml

Inside the files above, there are two lines:

<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk" />
<cmdReturns name="find $$ -name MacOSX10.10.sdk" />

Because of we using Xcode 7.1.1. Its SDKs string is ‘MacOSX10.11.sdk’. Add or change
those two lines as following:

<dirExists name="$$/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk" />
<cmdReturns name="find $$ -name MacOSX10.11.sdk" />

5. Modify mexopts.sh in the MATLAB’s bin directory. Take a look at the part of maci64
around line 160. Change the variables as following:

CC=’xcrun -sdk macosx10.11 clang’
MW_SDKROOT_TMP="find `xcode-select -print-path` -name MacOSX10.11.sdk"
MACOSX_DEPLOYMENT_TARGET=’10.11′
CXX=’xcrun -sdk macosx10.11 clang++’

Building binaries:

1. Open ‘Terminal’ application.

2. On prompt, type following commands:

$ cd libsvm-3.20/matlab
$ matlab -nodisplay

You will see something like this on the Terminal window:

Copyright 1984-2015 The MathWorks, Inc.
R2015b (8.6.0.267246) 64-bit (maci64)
August 20, 2015

To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.

>>

3. Beside that MATLAB command prompt, type ‘make’ command. There will output something messages
like following. You can ignore all warning messages:

Building with ‘Xcode with Clang’.
/Users/jslin/libsvm-3.20/matlab/libsvmread.c:202:5: warning: comparison of array
‘filename’ equal to a null pointer is always false [-Wtautological-pointer-compare]
if(filename == NULL)
^~~~~~~~    ~~~~
1 warning generated.

MEX completed successfully.
Building with ‘Xcode with Clang’.
MEX completed successfully.
Building with ‘Xcode Clang++’.
clang: warning: treating ‘c’ input as ‘c++’ when in C++ mode, this behavior is deprecated

clang: warning: treating ‘c’ input as ‘c++’ when in C++ mode, this behavior is deprecated

MEX completed successfully.
Building with ‘Xcode Clang++’.
clang: warning: treating ‘c’ input as ‘c++’ when in C++ mode, this behavior is deprecated
/Users/jslin/libsvm-3.20/matlab/svmpredict.c:17:36: warning: control reaches end of non-void function [-Wreturn-type]
int print_null(const char *s,…) {}
^
1 warning generated.

clang: warning: treating ‘c’ input as ‘c++’ when in C++ mode, this behavior is deprecated

MEX completed successfully.

4. After the compilation, exit MATLAB command mode with ‘exit’ command.

5. There are four files be added to the ‘libsvm-3.20/matlab’ folder:

libsvmread.mexmaci64
libsvmwrite.mexmaci64
svmpredict.mexmaci64
svmtrain.mexmaci64

6. Copy the whole folder of ‘libsvm-3.20’ into directory ‘/Applications/MATLAB_R2015b.app/toolbox/’.

7. For loading libsvm at startup, let’s open a file named ‘startup.m’ in the directory
‘/Applications//MATLAB_R2015b.app/toolbox/local’, and add a line as following:

addpath(‘/Applications/MATLAB_R2015b.app/toolbox/libsvm-3.20/matlab’);

8. Launch MATLAB GUI for test. Under the ‘Command Window’, you can try the examples that
inside libsvm README file. It should work like a charm.

Leave a Comment

Free Web Hosting