vindarmagnus vindarmagnus - 1 year ago 186
C Question

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

Answer Source

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):

clang -c\
    -DMX_COMPAT_32 \
    -I"/Applications/" \
    -I"/Applications/" \
    -fno-common \
    -arch x86_64 \
    -fexceptions \
    -O2 \
    -fwrapv \
    -DNDEBUG \
    "/Applications/" \
clang \
    -Wl,-twolevel_namespace \
    -undefined error \
    -arch x86_64 \
    -bundle  \
    -Wl,-exported_symbols_list,"/Applications/" \
    $(mex).o \
    c_mexapi_version.o  \
    -O \
    -Wl,-exported_symbols_list,"/Applications/"  \
    -L"/Applications/" \
    -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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download