Search code examples
c++matlabapimat

Using MATLAB API in C++, matOpen crashing program silently


my problem is similar to this one but it is rather old and I cannot comment or upvote due to my (beginners) reputation. I am trying to compile the matcreat.cpp file from Matlab 2017b like this (using g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0):

g++ -c -o matcreat.o -I"C:\Program Files\MATLAB\R2017b\extern\include" matcreat.cpp
g++ -o matcreat.exe matcreat.o -L"C:\Program Files\MATLAB\R2017b\extern\lib\win64\mingw64" -llibmat -llibmex -llibmx -llibeng

The program has a main which uses printf right at the beginning (even before the matlab stuff), but nothing is put out and it just terminates. If I comment out all the MATLAB related function calls it works, the first one being pmat = matOpen(file, "w");.

Why is the program just silently crashing and outputting nothing? Why no compiler/linker errors? Any ideas would be greatly appreciated.


Solution

  • After some time trying to work around the internals of the mex building process I gave up on that route. Mathworks is deliberately trying to make it hard to incorporate their features into your own code.

    For everyone with the same problems: After some research I found this open source project, which can write MAT files just fine.