Bug in MATLAB R2022a: MEX does not support gfortran 8.x as specified in the official documentation

16 views (last 30 days)
Update (20220406): The MathWorks MEX team has confirmed this problem as a bug of MATLAB R2022a. A workaround is provided by the MEX team, as detailed in the accepted answer.
------------------------
Is there any staff of MathWorks watching this forum? Thank you for having a look at this bug. I submitted a bug report, but no real response is received. No technical support is needed, but it would be great if MathWorks can fix this bug in the product --- or maybe correct the documentation mistake. Thank you very much.
------------------------
According to the official documentation, MATLAB R2022a supports gfortan8.x as the Fortran compiler for MEX on Linux. However, MATLAB R2022a with gfortran 8.3.0 on Ubuntu 20.04 fails to run the following code.
mex('-setup', '-v', 'FORTRAN');
mex('-v', fullfile(matlabroot, 'extern', 'examples', 'refbook', 'timestwo.F'));
The error message is
Error using mex
gfortran: error: unrecognized command line option ‘-fallow-argument-mismatch’; did you mean
-Wno-argument-mismatch?
The problem is reproduced on GitHub Actions. Nevertheless, gfortran 10 and 11 succeed, even though they are not supported officially.
Those who have MATLAB R2022a installed on Ubuntu may have a try on your side. Thank you very much!
------------------------
Update: Why does the error occur?
According to the documentations of gfortran 10.1.0 and gfortran 9.4.0, the -fallow-argument-mismatch flag was first introduced to gfortran in 10.1.0. Therefore, there is no hope that gfortran 8.x could recognize it.
Below are the MEX configuration files of MATLAB Version 9.12.0.1896817 (R2022a, Operating System: Linux 4.14.215-0414215-generic). As we can see, gfortran6.xml specifies -fallow-argument-mismatch as a compilation option. That is why gfortran 8.x cannot work with it.
BTW, another question: What is the relation and difference between gfortran6.xml and gfortran.xml? Which one defines the compilation options when MEX is invoked? In my investigation, it seems that gfortran6.xml is playing the role, although the name of this file is a bit confusing --- I thought it was the configration for gfortran 6 or below.
1. gfortran6.xml located at fullfile(matlabroot,'bin', 'glnxa64', 'mexopts', 'gfortran6.xml'):
<?xml version="1.0" encoding="UTF-8" ?>
<config
Name="gfortran"
ShortName="gfortran"
Manufacturer="GNU"
Version="$GFORTRAN_VERSION"
Language="FORTRAN"
Priority="A"
Location="$GFORTRAN_INSTALLDIR" >
<Details
CompilerExecutable="$FC"
CompilerDefines="$DEFINES"
CompilerFlags="$FFLAGS"
OptimizationFlags="$FOPTIMFLAGS"
DebugFlags="$FDEBUGFLAGS"
IncludeFlags="$INCLUDE"
LinkerLibraries="$LINKLIBS"
LinkerOptimizationFlags="$LDOPTIMFLAGS"
LinkerDebugFlags="$LDDEBUGFLAGS"
CompilerDefineFormatter="-D%s"
LinkerLibrarySwitchFormatter="-l%s"
LinkerPathFormatter="-L%s"
/>
<vars
CMDLINE1="$FC -c $DEFINES $INCLUDE $FFLAGS $OPTIM $SRC -o $OBJ"
CMDLINE2="$LDF $LDFLAGS $LDTYPE $LINKOPTIM $LINKEXPORTVER $OBJS $FLIBS $LINKLIBS -o $EXE"
FC="$GFORTRAN_INSTALLDIR/gfortran"
DEFINES=""
FFLAGS="-fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -fallow-argument-mismatch"
INCLUDE="-I&quot;$MATLABROOT/extern/include&quot; -I&quot;$MATLABROOT/simulink/include&quot;"
FOPTIMFLAGS="-O2"
FDEBUGFLAGS="-g"
LDF="$FC"
LDFLAGS="-pthread"
LDTYPE="-shared"
LINKEXPORT="-Wl,--version-script,$MATLABROOT/extern/lib/$ARCH/fexport.map"
LINKEXPORTVER="-Wl,--version-script,$MATLABROOT/extern/lib/$ARCH/fortran_exportsmexfileversion.map"
MWCPPLIB="&quot;$MATLABROOT/sys/os/$ARCH/orig/libstdc++.so.6&quot;"
LINKLIBS="-Wl,-rpath-link,$MATLABROOT/bin/$ARCH -L&quot;$MATLABROOT/sys/os/$ARCH/orig&quot; -L&quot;$MATLABROOT/bin/$ARCH&quot; $MWCPPLIB -lmx -lmex -lmat -lm -L&quot;$GFORTRAN_LIBDIR&quot; -lgfortran"
LDOPTIMFLAGS="-O"
LDDEBUGFLAGS="-g"
MW_GLIBC_SHIM="$MW_GLIBC_SHIM"
OBJEXT=".o"
LDEXT=".mexa64"
SETENV="FC=&quot;$FC&quot;
FFLAGS=&quot;$FFLAGS $DEFINES&quot;
FOPTIMFLAGS=&quot;$FOPTIMFLAGS&quot;
FDEBUGFLAGS=&quot;$FDEBUGFLAGS&quot;
LD=&quot;$FC&quot;
LDFLAGS=&quot;$LDFLAGS $LDTYPE $LINKLIBS $LINKEXPORT&quot;
LDDEBUGFLAGS=&quot;$LDDEBUGFLAGS&quot;">
</vars>
<!---Wl,-twolevel_namespace -undefined error $LDFEXPORT-->
<client>
<engine
LINKLIBS="$LINKLIBS $MW_GLIBC_SHIM -leng"
LINKEXPORT=""
LINKEXPORTVER=""
LDEXT=""
LDTYPE=""
/>
</client>
<locationFinder>
<GFORTRAN_INSTALLDIR>
<and>
<or>
<cmdReturns name="which gfortran" />
<fileExists name="/usr/local/bin/gfortran" />
</or>
<dirExists name="$$"/>
</and>
</GFORTRAN_INSTALLDIR>
<GFORTRAN_LIBDIR>
<and>
<or>
<cmdReturns name="which gfortran" />
<fileExists name="/usr/local/bin/gfortran" />
</or>
<cmdReturns name="gfortran -print-file-name=libgfortran.so" />
<dirExists name="$$" />
</and>
</GFORTRAN_LIBDIR>
<GFORTRAN_VERSION>
<and>
<or>
<cmdReturns name="which gfortran" />
<fileExists name="/usr/local/bin/gfortran" />
</or>
<cmdReturns name="gfortran -dumpversion | awk -F '.' '$1>=6' " />
</and>
</GFORTRAN_VERSION>
</locationFinder>
</config>
2. gfortran.xml located at fullfile(matlabroot,'bin', 'glnxa64', 'mexopts', 'gfortran.xml'):
<?xml version="1.0" encoding="UTF-8" ?>
<config
Name="gfortran6-"
ShortName="gfortran6-"
Manufacturer="GNU"
Version="$GFORTRAN_VERSION"
Language="FORTRAN"
Priority="B"
Location="$GFORTRAN_INSTALLDIR" >
<Details
CompilerExecutable="$FC"
CompilerDefines="$DEFINES"
CompilerFlags="$FFLAGS"
OptimizationFlags="$FOPTIMFLAGS"
DebugFlags="$FDEBUGFLAGS"
IncludeFlags="$INCLUDE"
LinkerLibraries="$LINKLIBS"
LinkerOptimizationFlags="$LDOPTIMFLAGS"
LinkerDebugFlags="$LDDEBUGFLAGS"
CompilerDefineFormatter="-D%s"
LinkerLibrarySwitchFormatter="-l%s"
LinkerPathFormatter="-L%s"
/>
<vars
CMDLINE1="$FC -c $DEFINES $INCLUDE $FFLAGS $OPTIM $SRC -o $OBJ"
CMDLINE2="$LDF $LDFLAGS $LDTYPE $LINKOPTIM $LINKEXPORTVER $OBJS $FLIBS $LINKLIBS -o $EXE"
FC="$GFORTRAN_INSTALLDIR/gfortran"
DEFINES=""
FFLAGS="-fexceptions -fbackslash -fPIC -fno-omit-frame-pointer"
INCLUDE="-I&quot;$MATLABROOT/extern/include&quot; -I&quot;$MATLABROOT/simulink/include&quot;"
FOPTIMFLAGS="-O2"
FDEBUGFLAGS="-g"
LDF="$FC"
LDFLAGS="-pthread"
LDTYPE="-shared"
LINKEXPORT="-Wl,--version-script,$MATLABROOT/extern/lib/$ARCH/fexport.map"
LINKEXPORTVER="-Wl,--version-script,$MATLABROOT/extern/lib/$ARCH/fortran_exportsmexfileversion.map"
MWCPPLIB="&quot;$MATLABROOT/sys/os/$ARCH/orig/libstdc++.so.6&quot;"
LINKLIBS="-Wl,-rpath-link,$MATLABROOT/bin/$ARCH -L&quot;$MATLABROOT/sys/os/$ARCH/orig&quot; -L&quot;$MATLABROOT/bin/$ARCH&quot; $MWCPPLIB -lmx -lmex -lmat -lm -L&quot;$GFORTRAN_LIBDIR&quot; -lgfortran -L&quot;$GFORTRANBEGIN_LIBDIR&quot; -lgfortranbegin"
LDOPTIMFLAGS="-O"
LDDEBUGFLAGS="-g"
MW_GLIBC_SHIM="$MW_GLIBC_SHIM"
OBJEXT=".o"
LDEXT=".mexa64"
SETENV="FC=&quot;$FC&quot;
FFLAGS=&quot;$FFLAGS $DEFINES&quot;
FOPTIMFLAGS=&quot;$FOPTIMFLAGS&quot;
FDEBUGFLAGS=&quot;$FDEBUGFLAGS&quot;
LD=&quot;$FC&quot;
LDFLAGS=&quot;$LDFLAGS $LDTYPE $LINKLIBS $LINKEXPORT&quot;
LDDEBUGFLAGS=&quot;$LDDEBUGFLAGS&quot;">
</vars>
<!---Wl,-twolevel_namespace -undefined error $LDFEXPORT-->
<client>
<engine
LINKLIBS="$LINKLIBS $MW_GLIBC_SHIM -leng"
LINKEXPORT=""
LINKEXPORTVER=""
LDEXT=""
LDTYPE=""
/>
</client>
<locationFinder>
<GFORTRAN_INSTALLDIR>
<and>
<or>
<cmdReturns name="which gfortran" />
<fileExists name="/usr/local/bin/gfortran" />
</or>
<dirExists name="$$"/>
</and>
</GFORTRAN_INSTALLDIR>
<GFORTRAN_LIBDIR>
<and>
<or>
<cmdReturns name="which gfortran" />
<fileExists name="/usr/local/bin/gfortran" />
</or>
<cmdReturns name="gfortran -print-file-name=libgfortran.so" />
<dirExists name="$$" />
</and>
</GFORTRAN_LIBDIR>
<GFORTRANBEGIN_LIBDIR>
<and>
<or>
<cmdReturns name="which gfortran" />
<fileExists name="/usr/local/bin/gfortran" />
</or>
<cmdReturns name="gfortran -print-file-name=libgfortranbegin.a" />
<dirExists name="$$" />
</and>
</GFORTRANBEGIN_LIBDIR>
<GFORTRAN_VERSION>
<and>
<or>
<cmdReturns name="which gfortran" />
<fileExists name="/usr/local/bin/gfortran" />
</or>
<cmdReturns name="gfortran -dumpversion" />
</and>
</GFORTRAN_VERSION>
</locationFinder>
</config>

Accepted Answer

Zaikun Zhang
Zaikun Zhang on 6 Apr 2022
Edited: Zaikun Zhang on 7 Apr 2022
According to MEX team (20220406, copied from a GitHub Issue):
This is bug with R2022a and we are fix it as soon as possible. These are the steps for a workaround for this bug. I am working on getting this fix in for R2022a but, it might take sometime. Meanwhile you are use this workaround in your scripts.
Please let me know if you run into any issues.
0. clean up the prefdir
>> delete(fullfile(prefdir,'mex_FORTRAN_glnxa64.xml'))
2. setup MEX compiler for Fortran
>> mex -setup FORTRAN
3. copy the MEX options file to the current directory
>> copyfile(fullfile(prefdir,'mex_FORTRAN_glnxa64.xml'), pwd)
4. Add the following to the <locationFinder> </locationFinder> section of the XML file mex_FORTRAN_glnxa64.xml
<GFORTRAN_EXECUTABLE >
<or>
<cmdReturns name="which gfortran" />
<fileExists name="/usr/local/bin/gfortran" />
</or>
</GFORTRAN_EXECUTABLE>
5. ReplaceFC = $GFORTRAN_INSTALLDIR/gfortranwithFC = $GFORTRAN_EXECUTABLE
With this change, you should be able to build MEX file
>> mex timestwo.F -v
>> MEX completed successfully.
This will fix all the MEX operations from this particular directory. For a persistent change, copy this file to the prefdir.
>> copyfile('mex_FORTRAN_glnxa64.xml', prefdir, 'f')
Note : Any command to setup the fortran compiler will over-write this file.

More Answers (0)

Categories

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

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!