Finally, Matlab R2026a allows mex to compile free-form Fortran code.
From https://uk.mathworks.com/help/matlab/ref/mex.html
Build MEX functions from free-form Fortran source files
The mex command builds MEX functions from both fixed-form and free-form Fortran (Fortran source file with a .F90 extension) source code.
I have a partial Matlab/Octave to Fortran transpiler here and have other transpilers to Fortran in the same project, at various levels of maturity.
Great! What a surprise. I have never expected that MathWorks would make any advancement concerning the mexification of Fortran, which has been the red-haired stepchild.
The Fortran backend of PRIMA is in the free form (of course), but it provides MEX interfaces for MATLAB. It achieves this by a preprocessor that refactors the free-form code into the intersection form, a form that is both free and fixed. For backward compatibility, it will continue to do so until we believe that most users are with MATLAB R2026a+.
Another observation: even before R2026a, MATLAB indeed supported the free form (unofficially) on Linux and macOS according to my tests. Windows + MATLAB + MEX = hell for developers, but there are many (really many) users living there, happily.
certik 4
How does the MEX compilation work exactly — don’t they compile it with a Fortran compiler anyway? So why would it matter if it is in free-form or fixed-form (as long as the Fortran compiler supports it, which I think all do)?
I’m not sure, but you cannot simply call a Fortran compiler because it would not recognize mwPointer.
I have two simple examples on my github page:
I guess they impose -fixed when calling Intel Fortran compiler on Windows. A very strange choice indeed.
In fortran syntax, this would be something like
type(mwPointer) :: plhs(*), prhs(*)
Perhaps this was done in Matlab before type() was added to fortran?
I thought mwPointer was just a preprocessor symbol that resolved to integer(8) (equal to integer(c_intptr_t) in practice).
From a copy of "fintrf.h" on GitHub:
#if defined(__x86_64__) || defined(_M_AMD64) || defined(__amd64) || \
defined(__sparcv9) || defined(__ppc64__)
# define mwpointer integer*8
# define mwPointer integer*8
# define MWPOINTER INTEGER*8
#else
# define mwpointer integer*4
# define mwPointer integer*4
# define MWPOINTER INTEGER*4
#endif
(Seems like it was an old copy, before ARM was a common thing; to my surprise, on Windows on ARM, MATLAB runs via an emulator → How emulation works on Arm | Microsoft Learn).
Machalot 9
This can be overridden, but it is an “advanced user” feature. Puzzling that they took so long with this illogical default.
You can copy fintrf.h from MATLAB to the same directory as your Fortran code, then you can compile the code using any Fortran compiler, but I have never tried linking.