MATLAB Answers

Compiling MEX Files without the mex Command

144 views (last 30 days)
MathWorks Support Team
MathWorks Support Team on 18 Jan 2018
Edited: David on 12 Feb 2021 at 18:01
How can I compile MEX Files without the mex Command?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 8 Jan 2019
Edited: MathWorks Support Team on 8 Jan 2019
OVERVIEW The mex command is the only supported way to compile MEX files. As an exception, you may compile MEX files without the mex command. For instance, if you use a compiler that is "unknown" to the mex command or you use an IDE that cannot be integrated with the mex command.
Below are the steps and examples on how to build a MEX file without the mex command.
Steps to Compile MEX Files without the Mex Command
1. Create a directory with MEX source files.
2. Add the MATLAB external include folder (matlabroot/extern/include) as an additional include folder and compile MEX sources with one or more MATLAB-specific API macros. The macro MATLAB_MEX_FILE should be used always; API-related macros are optional.
3. Link MEX object files with the MATLAB® dynamic libraies mx, mex, and mat.
4. Export from resulting shared library MATLAB entry points.
Steps for the Releases R2006b-R2016a
In the Releases R2006b-R2016a MEX files are compiled with one of two APIs. By default, MEX sources are compiled with large Array Dimensions. MEX sources are compiled with compatible Array Dimensions if the customer compiles with the macro MX_COMPAT_32.
Steps after the release R2016b
In the releases R2016b and later the customers need to add to the MEX sources one additional file from the directory matlabroot/extern/version:
  • c_mexapi_version.c - for C
  • cpp_mexapi_version.cpp - for C++
  • fortran_mexapi_version.F - for FORTRAN\
Steps in R2018a
In the release R2018a we introduce the API interleaved complex and the macro that controls it:
  • MATLAB_DEFAULT_RELEASE=R2017b - for the large Array Dimensions API
  • MATLAB_DEFAULT_RELEASE=R2018a - for the interleaved complex API
Compiling MEX File with the Microsoft Visual IDE VS 2015 in R2018a
  • Create a project and insert your MEX source files. In the release R2016b and later add to your MEX sources one version file from the directory matlab\extern\versions. These are the files to choose from:
  • On the Project menu, click Properties for the project to open the property pages.
  • Under C/C++ General properties, add the MATLAB® include folder, matlab\extern\include, as an additional include folder.
  • Under C/C++ Preprocessor properties, add MATLAB_MEX_FILE as a preprocessor definition.
Add the macro MX_COMPAT_32 for the compatible array dimensions API.
Add the macro MATLAB_DEFAULT_RELEASE=R2017b for the large array dimensions API.
Add the macro MATLAB_DEFAULT_RELEASE=R2018a for interleaved complex API.
  • Under Linker General properties, change the output file extension to .mexw64.
  • Locate the .lib files for the compiler you are using under matlabroot\extern\lib\win64\microsoft and under Linker Input properties, add libmx.lib, libmex.lib, and libmat.lib as additional dependencies.
  • Create a .def file to export the MEX entry point(s). On the Project menu, click Add New Item and select Module-Definition File (.def). Here is the .def file for the releases R2016a and before:
EXPORTS mexFunction (_MEXFUNCTION for a Fortran MEX file)
For the releases R2016b and later add the following entry point:
EXPORTS mexfilerequiredapiversion (_MEXFILEREQUIREDAPIVERSION for a Fortran MEX file)
  • Under Linker Input properties, add the module definition (.def) file you created.
  • Under Linker Debugging properties, if you intend to debug the MEX file using the IDE, specify that the build generates debugging information. For more information about debugging, see Debug on Microsoft Windows Platforms.
Compiling MEX File on Windows with a compiler other than the Microsoft Visual C++ in R2018a
If you are using a compiler other than the Microsoft Visual C++ compiler, the process for building MEX files is similar. Locate the .lib files for the compiler you are using in the folder matlabroot\extern\lib\win64.
Compiling MEX Files on Linux in R2018a with gcc
  • Set the environment variable MWROOT to the MATLAB R2018a installation root directory.
  • Copy your MEX sources to the directory mysrc.
  • Copy the file $MWROOT/extern/version/c_mexapi_version.c to the directory mysrc.
  • Copy the following file Makefile to the directory mysrc.
# Custom MEX Makefile for MATLAB R2018a
CC := gcc
# Matrix APIs:
# -DMX_COMPAT_32: compatibleArrayDims
# -DMATLAB_DEFAULT_RELEASE=R2017b: largeArrayDims
# -DMATLAB_DEFAULT_RELEASE=R2018a: Interleaved Complex
CFLAGS := -fexceptions -fPIC -fno-omit-frame-pointer -pthread
INCLUDE := -I"$(MWROOT)/extern/include" -I"$(MWROOT)/simulink/include"
LD := gcc
LDFLAGS := -pthread -Wl,--no-undefined
LDTYPE := -shared
LINKEXPORTVER := -Wl,--version-script,"$(MWROOT)/extern/lib/glnxa64/"
LINKLIBS := -Wl,--as-needed -Wl,-rpath-link,$(MWROOT)/bin/glnxa64 -L"$(MWROOT)/bin/glnxa64" -Wl,-rpath-link,$(MWROOT)/extern/bin/glnxa64 -L"$(MWROOT)/extern/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++
mexfunction.mexa64 : mexfunction.o c_mexapi_version.o
$(LD) $(LDFLAGS) $(LDTYPE) $(LINKEXPORTVER) mexfunction.o c_mexapi_version.o $(LINKLIBS) -o $@
c_mexapi_version.o : c_mexapi_version.c Makefile
mexfunction.o : mexfunction.cpp Makefile

  1 Comment

David on 12 Feb 2021 at 2:40
I could very well be wrong, but I believe that in the answer, the comments for the pre-processor symbols are not quite right. Shouln't it be:
# -DMX_COMPAT_32: compatibleArrayDims
# -DMX_COMPAT_64: largeArrayDims
# -DMATLAB_DEFAULT_RELEASE=R2017b: Separate Complex
# -DMATLAB_DEFAULT_RELEASE=R2018a: Interleaved Complex
As a side note, it might be that MATLAB_DEFAULT_RELEASE=R2018a should only ever be used with MX_COMPAT_64, whereas MATLAB_DEFAULT_RELEASE=R2017b can be used with either MX_COMPAT_32 or MX_COMPAT_64 (TBC).

Sign in to comment.

More Answers (1)

Silvio Traversaro
Silvio Traversaro on 16 Jan 2019
Thanks for the really useful piece of documentation. One question: what are the "Matlab/MEX entry points" symbols that is necessary to expose in the mex file? For Windows you document mexFunction (and mexfilerequiredapiversion since 2016b), but at least on Linux on 2018a it seems that also mexCreateMexFunction, mexDestroyMexFunction and mexFunctionAdapter are exported.


Sign in to comment.





Community Treasure Hunt

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

Start Hunting!