Is it possible to build FORTRAN-only mex-files using GNU FORTRAN (gfortran) supplied with MinGW-w64 under Windows 10?
6 views (last 30 days)
Show older comments
Hello all,
I have a question about compiler support for Mex with Matlab R2018a under 64-bit Windows 10. We have a Matlab mex function that uses mixed C and FORTRAN source code. The mex gateway is written in C. Using the mingw-w64 compilers found as an Add-On in Matlab, this function compiles, links and works fine.
On the supported compilers page it is stated that the mingw-w64 C/C++ compilers are supported and that you can get them here: MinGW-w64 at the File Exchange. It is not stated that the GNU FORTRAN compiler (gfortran) is supported, but it is actually part of the distribution and the experience with our mixed C/FORTRAN mex-file above confirms that it works.
In the "Comments and Ratings" part of the MinGW-w64 page on the File Exchange the "Mathworks Supported Compilers Team" on 2017-07-18 said GNU FORTRAN is supported.
Now to my problem and question. I am trying to build a FORTRAN-only mex-file, but without success. It compiles fine, but when linking it fails. Furthermore, trying one of the supplied sample files, timestwo.F, doesn't work either. When compiling,
mex -c timestwo.F
I get:
Warning: Selected compiler 'MinGW64 Compiler (FORTRAN)' is not supported and no other supported compiler was found. For options, visit https://www.mathworks.com/support/compilers.
Building with 'MinGW64 Compiler (FORTRAN)'.
MEX completed successfully.
So mex reports that the compiler is not supported, even though it is said to be supported on the file exchange as per above. Also, the file is compiled and I get a timestwo.obj.
Trying to build the mex-file,
mex timestwo.F
I get:
Warning: Selected compiler 'MinGW64 Compiler (FORTRAN)' is not supported and no other supported compiler was found. For options, visit https://www.mathworks.com/support/compilers.
Error using mex
Cannot export mexFunction: symbol not defined
timestwo.obj:timestwo.F:(.text+0x43): undefined reference to `mexerrmsgidandtxt'
timestwo.obj:timestwo.F:(.text+0x69): undefined reference to `mexerrmsgidandtxt'
timestwo.obj:timestwo.F:(.text+0x71): undefined reference to `mxisnumeric'
timestwo.obj:timestwo.F:(.text+0x94): undefined reference to `mexerrmsgidandtxt'
timestwo.obj:timestwo.F:(.text+0x9c): undefined reference to `mxgetm730'
timestwo.obj:timestwo.F:(.text+0xa7): undefined reference to `mxgetn730'
timestwo.obj:timestwo.F:(.text+0xb9): undefined reference to `mxgetpr'
timestwo.obj:timestwo.F:(.text+0xd0): undefined reference to `mxcopyptrtoreal8730'
timestwo.obj:timestwo.F:(.text+0xe4): undefined reference to `mxcreatedoublematrix730'
timestwo.obj:timestwo.F:(.text+0xee): undefined reference to `mxgetpr'
timestwo.obj:timestwo.F:(.text+0x10f): undefined reference to `mxcopyreal8toptr730'
collect2.exe: error: ld returned 1 exit status
So my question is, is it possible to use the MinGW-w64 compiler with FORTRAN-only mex-files? It would (of course) be fantastic if it did, and I would be very grateful if someone could point me in the right direction.
Thanks for any input on this!
Bernt
0 Comments
Accepted Answer
Sid Jhaveri
on 9 May 2018
Hi Bernt,
Currently, MATLAB supports GNU FORTRAN only in Linux. MATLAB supports MinGW-w64 on Windows only for C/C++. For Windows, Intel Parallel studio XE and Intel Visual Fortran are supported Fortran compiler in MATLAB. For more information on supported compilers, you can visit https://www.mathworks.com/support/compilers.html
8 Comments
dpb
on 7 Dec 2021
To have the FORTRAN part of a mixed C/FORTRAN mex-file work I used the XML-file for (mingw-w64) C and made "obvious" changes to it (like defining CC as gfortran instead of gcc etc) and a maybe not so obvious change (add "-fno-underscoring" to CFLAGS), and saved it to a new file. This file is then used with the -f switch of the mex-command."
But, with gfortran 6.3.0, adding -fno-underscoring then results in the exported entry names being append with "800" without the underscore.
Looking in, for example, libmex.lib, there are entry points exported with no trailing decoration or with _800, but none with only the trailing underscore gfortran emits by default nor with just "800".
That's a cursory look into trying to build "timestwo.F" from command line with the gfortran build included in the installed GCC that the MATLAB GCC link downloads.
gfortran runs just fine but I guess this linking issue is one TMW doesn't want to work through, although if they can do it for Linux/MacOS, doesn't seem as though should be any fundamental issue to include same for Windwoes since they can and do link C.
All in all, it still is an affront to their users and Fortran in general...there are other commerical Fortran compilers out there as well besides Intel although they are, granted, clearly the leader.
dpb
on 8 Dec 2021
As a note for anybody else digging in the area who stumbles across this --
Indeed, the "trick" of using the MinGW XML file for GCC as the template did work and then (at least with R2020b) mex -setup FORTRAN will find and use it for mex compilation. That is a step forward although linking the Fortran generated code to the libraries is still an issue as outlined above for all Fortran.
However, it seems to me that since the libraries were built by TMW with and successfully link to the gcc C object files, one should be able to use the new C interop features of Fortran and build object fiies that will interoperate with the existing libraries. It would be far better if TMW would build/add the direct Fortran entry points/linkages, but it seems as though should be possible with the existing libraries.
At this point, however, I have not used gfortran sufficiently long nor am I yet familiar-enough with C interop features to be able to just throw something together; I'll have to research this at some length to give it a go.
See Also
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!