Are Fortran mex files restricted to fixed format?

2 views (last 30 days)
I'm learning to mex Fortran files and am having trouble with the timestwo.F tutorial. I'm able to compile and run the built-in, fixed format example file without a problem. However, when I tried to compile my own free format file named timestwo.f90, I got a bunch of errors about invalid preprocessor directives.
From a MATLAB Answers question, I found that I needed to capitalize the f in the file extension. mex timestwo.F90 completes successfully. When I try to use the function, though, I get an error:
Invalid MEX-file '/home/user/Documents/MATLAB/mex/timestwo.mexa64': /home/user/Documents/MATLAB/mex/timestwo.mexa64: undefined
symbol: mxgetdoubles_
The only difference between my file and the example file is that mine is free format and doesn't include the #if MX_HAS_INTERLEAVED_COMPLEX check found in the example file. Is this an issue with support for free format source?

Answers (1)

Josh G.
Josh G. on 28 Mar 2019
I figured this out a few minutes after posting my question by trying something I thought I had already tried: adding the -R2018a option. My guess is I had to both add -R2018a to the mex options and change the file extension to .F90, which I had probably tried individually but not simultaneously. I'll leave the question up in case someone has this same problem in the future when getting into using Fortran mex files.
  2 Comments
James Tursa
James Tursa on 3 Apr 2019
Edited: James Tursa on 3 Apr 2019
The -R2018a option is related to whether you are using the old separate complex data format interface or the new interleaved complex data format interface. This has nothing to do with your fixed or free format source code problems.
In the past, the free format problem has been caused by MATLAB having a "/fixed" COMPFLAGS option in their build files, forcing the compilation to be fixed format regardless of extension. Alas, I just checked a Fortran build file for version R2018b WIN64 and this silly option is still there in spite of my repeated attempts at getting TMW to remove it. This may be the root cause of your fixed/free problems. The solution is to edit the build files (e.g., intel_fortran_18_vs2017.xml) and remove the "/fixed" option from the COMPFLAGS line. That will let the compiler choose fixed or free format based on file extension (.f or .f90 respectively) as it should be. I wouldn't think the compiler would care about upper/lower case for the extension, but maybe something in the MATLAB build scripts does.
@TMW, are you ever going to remove that "/fixed" option? It only causes problems!
Josh G.
Josh G. on 8 Apr 2019
Using the -R2018a option was why my code started working, but you're right that it has nothing to do with formatting.
It turns out mxGetDoubles was introduced in R2018b; you have to use mxGetPr for pre-R2018b compatibility.
I'm on Linux, so fortunately I don't have a fixed format flag built into the gfortran mexopts file, but over the past week I've had to work on a Windows system a good bit and have had to deal with repeatedly removing the /fixed flag while resetting the compiler configuration. It's beyond me why they would include that when it doesn't cause any problems to remove it.

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!