I would like to compile MEX-files (MATLAB executable) in CLion instead of in MATLAB (which gives no help on writing C code). However, MEX-files require the #include mex.h(pp), which is not available on normal C++. Moreover, the format, which does not have a main
function, is different.
I am using MATLAB R2018b and hoping to use C++11 using the new C++ API for MEX-files. However, I would be able to use the old API as well.
I have tried looking at CMake's FindMatlab module and in several other locations. However, most other guides are out of date and even their reference links do not connect to their original pages.
I am an absolute newbie at CMake and I don't know where to begin.
I am currently unable to use #include "mex.h"
, #include "mex.hpp"
, #include "mexAdapter.hpp"
etc. I am also unable to compile a function without a main
function.
Many thanks in advance for anyone who can help by uploading or describing the CMakeLists.txt
file that would be necessary.
To compile a MEX-file, your CMake file should contain:
find_package(Matlab)
matlab_add_mex(NAME mex_file_name SRC source_file.cpp)
mex_file_name
is the name of the target, the extension is added automatically. This is a normal target, you can use set_target_properties
, target_compile_definitions
, etc. on that target.
If your MEX-file needs to link to a library, add LINK_TO library
at the end of the matlab_add_mex
command.
Regarding using the new C++ API: I don't remember if it is necessary to add the R2018a
flag to the matlab_add_mex
call. This flag is necessary when using the new C API (complex interleaved) as opposed to the old C API (separate complex). I think it is not necessary for the C++ API, but if things don't compile, add this flag to see if it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With