Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab mex without xcode, but with standalone command line tools

I want to compile mex files without installing xcode, using only Command Line Tools (from apple developer center).

Apple Command Line Tools install the compiler and adds standard libraries and headers to the system in a package much smaller than xcode (which is several GBs).

Running mex on linux is possible - I see no reason why matlab mex should require the huge SDKs required for macos. A long evening of trial and error and hacking configuration files hasn't helped. Does anyone have a minimal working example of how to compile a mex file outside matlab, or a simple way to use mex without having xcode installed?

Best Regards, Magnus

like image 922
vindarmagnus Avatar asked Sep 18 '25 08:09

vindarmagnus


2 Answers

Well, I have another option here: Edit the files under /Applications/MATLAB_R2016b.app/bin/maci64/mexopts (probably there should be 3.xml file, all needs the same modification).

Locate the <XCODE_AGREED_VERSION> portion, comment the whole xml tag, e.g. wrap them with <!-- and --> like this:

<!--XCODE_AGREED_VERSION>
            <and diagnostic="Xcode is installed, but its license has not been accepted. Run Xcode and accept its license agreement." >
                <or>
                    <cmdReturns name="defaults read com.apple.dt.Xcode IDEXcodeVersionForAgreedToGMLicense"/>
                    <cmdReturns name="defaults read /Library/Preferences/com.apple.dt.Xcode IDEXcodeVersionForAgreedToGMLicense"/>
                </or>
                <cmdReturns name="&#10;agreed=$$ &#10; if echo $agreed | grep -E '[\.\&quot;]' >/dev/null; then &#10; lhs=`expr &quot;$agreed&quot; : '\([0-9]*\)[\.].*'` &#10;  rhs=`expr &quot;$agreed&quot; : '[0-9]*[\.]\(.*\)$'` &#10; if echo $rhs | grep -E '[\.&quot;]' >/dev/null; then &#10; rhs=`expr &quot;$rhs&quot; : '\([0-9]*\)[\.].*'` &#10; fi &#10; if [ $lhs -gt 4 ] || ( [ $lhs -eq 4 ] &amp;&amp; [ $rhs -ge 3 ] ); then &#10; echo $agreed &#10; else &#10; exit 1&#10; fi &#10; fi" />
            </and>
        </XCODE_AGREED_VERSION -->

Some notes:

  1. These files are read only by default, you need to issue sudo chmod 644 * in that directory

  2. after comment out all necessary files, issue this command in matlab: mex -setup C++

Then you are done

like image 123
victl Avatar answered Sep 20 '25 23:09

victl


After spending more time, I wound up learning more stuff and answering my own question. I'll post my solution here if anyone else needs it in the future.

Make sure the cord is connected to your computer and that MATLAB is installed, and also install the command line tools from apple. Then call the following makefile to compile arrayProduct.c (comes with matlab) from the terminal as follows:

make mex=arrayProduct

Put this makefile code in the same folder in a file called makefile(edit to your own needs if you have to):

all:
clang -c\
    -DMX_COMPAT_32 \
    -DMATLAB_MEX_FILE \
    -I"/Applications/MATLAB_R2016b.app/extern/include" \
    -I"/Applications/MATLAB_R2016b.app/simulink/include" \
    -fno-common \
    -arch x86_64 \
    -fexceptions \
    -O2 \
    -fwrapv \
    -DNDEBUG \
    "/Applications/MATLAB_R2016b.app/extern/version/c_mexapi_version.c" \
    $(mex).c
clang \
    -Wl,-twolevel_namespace \
    -undefined error \
    -arch x86_64 \
    -bundle  \
    -Wl,-exported_symbols_list,"/Applications/MATLAB_R2016b.app/extern/lib/maci64/mexFunction.map" \
    $(mex).o \
    c_mexapi_version.o  \
    -O \
    -Wl,-exported_symbols_list,"/Applications/MATLAB_R2016b.app/extern/lib/maci64/c_exportsmexfileversion.map"  \
    -L"/Applications/MATLAB_R2016b.app/bin/maci64" \
    -lmx \
    -lmex \
    -lmat \
    -lc++ \
    -o $(mex).mexmaci64

The above makefile is a bare minimum working example, you should edit it to comply with your requirements.

Edit: Option 2 You can make MATLAB understand how to use the Command Line Tools by editing the xml file containing the compiler options instead. Open the file located at /User/username/Library/Application Support/MathWorks/MATLAB/R2016b/mex_C_maci64.xml

Remove all compiler and linker options related to ISYSROOT. This will make the compiler search for header files in /usr/include etc instead of in the SDK-folder in XCode.

like image 22
vindarmagnus Avatar answered Sep 20 '25 22:09

vindarmagnus