4

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

vindarmagnus
  • 326
  • 3
  • 10
  • I think you can install `gcc` from homebrew and essentially use the same compiler as linux. I have both on my machine and I can check later if matlab is able to see both. – mpaskov Oct 14 '16 at 21:52
  • Actually I do not think that gcc is supported on mac, I can only use xcode, sorry. – mpaskov Oct 15 '16 at 01:10
  • Thanks anyway! As you see below, I found the solution myself. – vindarmagnus Oct 17 '16 at 09:48
  • I happened to stumble on this topic [gcc mex](http://stackoverflow.com/questions/32155724/gcc-c-c-mex-matlab-r2015-mac-os-x-with-openmp-doesnt-work). Have not tried it but it might be interesting. – mpaskov Oct 18 '16 at 17:34
  • 1
    Thanks! For now however, Option 2 in my own solution below is working perfectly! – vindarmagnus Oct 19 '16 at 18:18
  • Another option is to install Homebrew and then fetch GCC. This will allow you to make MEX-files that use OpenMP. You need to compile the MEX-file without using the `mex` command, as in the accepted answer. See also [this answer on Matlab Answers](https://www.mathworks.com/matlabcentral/answers/377799-compiling-mex-files-without-the-mex-command). – Cris Luengo Sep 21 '18 at 14:40

3 Answers3

7

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

victl
  • 161
  • 1
  • 6
  • After 'investing' a couple of hours trying to figure out this issue, I'd like to say thank you for the solution. – AnweshCR7 Apr 06 '21 at 13:39
  • Thanks so much! I can confirm it still works with later version (2019b) – boris Nov 29 '21 at 13:00
  • This still worked in MATLAB 2021b, thank you! – The Tentacle Jan 07 '22 at 11:26
  • I do not really understand the notes, is the code `sudo chmod 644 *` and `mex -setup C++`has to be run in Terminal? The directory is where xml files are located? For exemple /application/MATLAB_R2021b.app/bin/maci64/mexopts – C. Guff May 12 '22 at 15:32
4

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.

vindarmagnus
  • 326
  • 3
  • 10
  • Thanks, the solution (option 2) works on my machine. – Eli4ph Jan 23 '18 at 09:23
  • You might not want to use `MX_COMPAT_32`, unless you MEX-file source code is really old and uses `int` instead of `mwSize` and the like. For more information about what macros to define during compilation, see [this question on Matlab Answers](https://www.mathworks.com/matlabcentral/answers/377799-compiling-mex-files-without-the-mex-command). – Cris Luengo Sep 21 '18 at 14:38
2

I fully understand you and I'm a big advocate of non-using the enormous Xcode. From my saver here, run the following to trick mex into seeing an accepted license (no sudo needed). Here I use the current version 13.0 at the time of writing, to be adapted.

defaults write com.apple.dt.Xcode IDEXcodeVersionForAgreedToGMLicense 13.0
jmon12
  • 1,090
  • 8
  • 17