9

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.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
veritas9872
  • 101
  • 2
  • 5
  • I do not know how CLion works, but can you not create a "myCompile" keyword that would call the MATLAB compiler? I am not sure if you can compile from outside it. – Ander Biguri Feb 19 '19 at 09:56
  • 2
    You may start with command [matlab_add_mex](https://cmake.org/cmake/help/v3.14/module/FindMatlab.html#command:matlab_add_mex). Before using it, you need to call `find_package(Matlab)`. Usage of this command is very similar to "standard" `add_executable` or `add_library`. – Tsyvarev Feb 19 '19 at 09:59

1 Answers1

5

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.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • However, I have now found errors such as undefined reference to `mxGetSingles_800' popping up all over the place. I have tried including matrix.h with #include "matrix.h" at the top but it still does not work. – veritas9872 Sep 02 '19 at 06:51
  • @veritas9872: please ask a new question for that, including a [mre]. It is impossible for me to know what is wrong from the short description in your comment. – Cris Luengo Sep 02 '19 at 12:27
  • 1
    I had difficulty finding MATLAB(64 bit) on my windows. Apparently the problem seems to be the default platform is set to x86. Adding this line to my CMakeLists.txt `set(CMAKE_GENERATOR_PLATFORM x64)` or passing the same definition while configuring solved it. – Praveen Jun 29 '21 at 12:46