I'm working on getting gfortran working in place of the Intel Fortran compiler for MATLAB on Windows 10. The GCC style of name mangling, at least for gfortran, is to make the symbol name all lowercase and append an underscore; "mxIsNumeric800" becomes mxisnumeric800_. To get MATLAB to recognize the symbols, they have to omit the appended underscore (easy, just add -fno-underscoring
to the compiler options) and consist entirely of uppercase alphanumeric characters (difficult, but possible).
I've been able to fix this capitalization issue by using the method described in this answer, but I can't get the exported symbols to remain uppercase. Here are the exported symbols for a mex file compiled with gfortran (part of output of dumpbin /exports timestwo.mexw64
):
ordinal hint RVA name
1 0 00001510 mexfilerequiredapiversion
2 1 000013E0 mexfunction
3 2 000013C0 timestwo
and the same file compiled with the Intel Fortran compiler:
ordinal hint RVA name
1 0 00001150 MEXFILEREQUIREDAPIVERSION
2 1 00001000 MEXFUNCTION
The Intel compiler handles exports via a linker option /EXPORT:FOO
which is used twice, once for MEXFUNCTION and once for MEXFILEREQUIREDAPIVERSION. I've tried using a .def file consisting of
EXPORTS
MEXFUNCTION
MEXFILEREQUIREDAPIVERSION
but linking it (as opposed to a .def file with lowercase or camelcase symbols) doesn't change anything. I've also tried adding to my linker script:
EXTERN(MEXFUNCTION,MEXFILEREQUIREDAPIVERSION);
PROVIDE(mexfunction = MEXFUNCTION);
PROVIDE(mexfilerequiredapiversion = MEXFILEREQUIREDAPIVERSION);
but this doesn't change anything.
How can I modify these symbols to be uppercase?
I've solved the specific problem I was having, but the solution isn't satisfactory (gfortran-compiled mex files still cause MATLAB to crash).
As it turns out, there are a few ways to accomplish manual symbol capitalization. One, which I thought didn't work but have since discovered otherwise, is BIND(C,NAME="")
. A mex function like subroutine mexfunction(nlhs, plhs, nrhs, prhs) bind(C,name="MEXFUNCTION")
will result in an uppercase MEXFUNCTION symbol which can be exported.
An alternate way is to alias the symbol in an export definition (.def) file. A file containing
EXPORTS
MEXFUNCTION = mexfunction
MEXFILEREQUIREDAPIVERSION = mexfilerequiredapiversion
included when running the linker will also result in the desired symbols being exported. Either of these is a solution to the question, even if it's not a solution to the overall problem I'm trying to solve.
Using either of these methods results in a mex function that crashes MATLAB when run. The crash occurs when memcpy
from VCRUNTIME140.dll runs at the beginning of the mex function, resulting in an access violation. I was able to get MATLAB to output a crash dump by building the mex function with debugging symbols.
Unfortunately, it looks like there's even more to getting gfortran working with MATLAB than just linking the correct libraries and using the correct symbols.