Compiling MEX Files without the mex Command

47 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2018 年 1 月 18 日
コメント済み: Markus Leuthold 2024 年 5 月 28 日
How can I compile MEX Files without the mex Command?

採用された回答

MathWorks Support Team
MathWorks Support Team 2019 年 1 月 8 日
編集済み: MathWorks Support Team 2018 年 3 月 22 日
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)
  4 件のコメント
Markus Leuthold
Markus Leuthold 2024 年 5 月 22 日
編集済み: Markus Leuthold 2024 年 5 月 22 日
I am able to compile a mex with MS Visual Studio 22 in C and mexFunction() according to the description in this answer. However, I fail compiling a C++ class derived from matlab::mex::Function.
Sample code
#include "mex.hpp"
#include "mexAdapter.hpp"
class MexFunction : public matlab::mex::Function {
public:
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs)
{
}
};
Error message
What additional steps do I miss?
Markus Leuthold
Markus Leuthold 2024 年 5 月 28 日
According to Mathworks support, you need to add libMatlabDataArray.lib (in addition to libmx.lib, libmex.lib, and libmat.lib) as a linker dependency. This lets you successfully compile a cpp mex in visual studio

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by