Compiling MEX Files without the mex Command

82 views (last 30 days)
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 22 Mar 2018
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
EXAMPLES
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:
c_mexapi_version.c
cpp_mexapi_version.cpp
fortran_mexapi_version.F
  • 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:
LIBRARY MYFILE
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
DEFINES := -D_GNU_SOURCE
MATLABMEX := -DMATLAB_MEX_FILE
# Matrix APIs:
# -DMX_COMPAT_32: compatibleArrayDims
# -DMATLAB_DEFAULT_RELEASE=R2017b: largeArrayDims
# -DMATLAB_DEFAULT_RELEASE=R2018a: Interleaved Complex
MATLABMEX += -DMX_COMPAT_32
CFLAGS := -fexceptions -fPIC -fno-omit-frame-pointer -pthread
COPTIMFLAGS := -O -DNDEBUG
CDEBUGFLAGS := -g
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/c_exportsmexfileversion.map"
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
$(CC) -c $< -o $@ $(MATLABMEX) $(DEFINES) $(CFLAGS) $(COPTIMFLAGS) $(INCLUDE)
mexfunction.o : mexfunction.cpp Makefile
$(CC) -c $< -o $@ $(MATLABMEX) $(DEFINES) $(CFLAGS) $(COPTIMFLAGS) $(INCLUDE)
  2 Comments
Cris Luengo
Cris Luengo on 6 Dec 2021
That is right, the "Interleaved Complex" API is only 64-bit.
dpb
dpb on 8 Sep 2023
Edited: dpb on 8 Sep 2023
<This link in current (R2023a) release> has the mind-numbing information that "MATLAB supports MEX files written in Fortran 77."
Come on, now, MW, besides the page itself not having been updated since R2018 days, Fortran is now at official standard 2018 with another update targetted for 2023. Even F95 is now almost 30 years since its introduction and 1977 was now 46(!!!!) years ago. It's long since past time to get on with it.
Particularly with the introduction of C interoperability, there's no excuse for continuing to hamstring the mex library to the base data classes.
If nothing else, give the Fortran community the ability to build their own libraries.

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!