MATLAB gives “undefined reference” errors trying to use gfortran on Windows

南楼画角 提交于 2019-12-25 01:15:15

问题


I'm trying to use MinGW gfortran with MATLAB R2019a on Windows 10 by writing a mexopts XML file based on existing XML files. I have cygwin installed with MinGW gcc, and I've been able to run mex -setup referencing the XML file successfully. When I try to compile the timestwo.F file to test the setup (using mex -R2018a timestwo.F90), I get a bunch of errors:

Building with 'cygwin MinGW64 Compiler (Fortran)'.
C:\cygwin64\bin\x86_64-w64-mingw32-gfortran -c -DMX_COMPAT_64  -DMATLAB_MEXCMD_RELEASE=R2018a  -DUSE_MEX_CMD    -I"C:\Program Files\MATLAB\R2019a\extern\include" -I"C:\Program Files\MATLAB\R2019a\simulink\include" -I"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64" -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -O3 -mtune=native "C:\cygwin64\home\user\MATLAB\timestwo.F90" -o C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o
f951: Warning: Nonexistent include directory ‘C:\Program Files\MATLAB\R2019a\simulink\include’ [-Wmissing-include-dirs]

C:\cygwin64\bin\x86_64-w64-mingw32-gfortran -c -DMX_COMPAT_64  -DMATLAB_MEXCMD_RELEASE=R2018a  -DUSE_MEX_CMD    -I"C:\Program Files\MATLAB\R2019a\extern\include" -I"C:\Program Files\MATLAB\R2019a\simulink\include" -I"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64" -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -O3 -mtune=native "C:\Program Files\MATLAB\R2019a\extern\version\fortran_mexapi_version.F" -o C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\fortran_mexapi_version.o
f951: Warning: Nonexistent include directory ‘C:\Program Files\MATLAB\R2019a\simulink\include’ [-Wmissing-include-dirs]

C:\cygwin64\bin\x86_64-w64-mingw32-gfortran -pthread -shared -O3 -mtune=native -Wl,--version-script,"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64\fortran_exportsmexfileversion.map" C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\fortran_mexapi_version.o   -Wl,-L"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64" -llibmx -llibmex -llibmat -lm -L"C:\cygwin64\lib\gcc\x86_64-w64-mingw32\7.4.0" -lgfortran -o timestwo.mexw64
Error using mex
C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o:timestwo.F90:(.text+0x47): undefined reference to
`mxisnumeric800_'
C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o:timestwo.F90:(.text+0x6a): undefined reference to
`mexerrmsgidandtxt800_'
C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o:timestwo.F90:(.text+0x72): undefined reference to
`mxgetdoubles800_'
C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o:timestwo.F90:(.text+0x7d): undefined reference to `mxgetm800_'
C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o:timestwo.F90:(.text+0x8c): undefined reference to `mxgetn800_'
C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o:timestwo.F90:(.text+0xa6): undefined reference to
`mxcopyptrtoreal8800_'
C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o:timestwo.F90:(.text+0xba): undefined reference to
`mxcreatedoublematrix800_'
C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o:timestwo.F90:(.text+0xc4): undefined reference to
`mxgetdoubles800_'
C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o:timestwo.F90:(.text+0xe5): undefined reference to
`mxcopyreal8toptr800_'
C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o:timestwo.F90:(.text+0x10d): undefined reference to
`mexerrmsgidandtxt800_'
C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o:timestwo.F90:(.text+0x13b): undefined reference to
`mexerrmsgidandtxt800_'
collect2: error: ld returned 1 exit status

Here's my source:

! Gateway routine
#include "fintrf.h"

subroutine mexFunction(nlhs, plhs, nrhs, prhs)
implicit none

mwPointer plhs(*), prhs(*)
integer nlhs, nrhs

! Declarations
mwPointer mxGetDoubles
mwPointer mxCreateDoubleMatrix
integer mxIsNumeric
mwPointer mxGetM, mxGetN

mwPointer x_ptr, y_ptr

mwPointer mrows, ncols
mwSize size

! Declare variables for computational routine
real*8 x_input, y_output

! Make sure input is valid
if(nrhs /= 1) then
    call mexErrMsgIdAndTxt ('MATLAB:timestwo:nInput', &
                            'One input required.')
elseif(nlhs > 1) then
    call mexErrMsgIdAndTxt ('MATLAB:timestwo:nOutput', &
                            'Too many output arguments.')
endif

if(mxIsNumeric(prhs(1)) == 0) then
    call mexErrMsgIdAndTxt ('MATLAB:timestwo:NonNumeric', &
                            'Input must be a number.')
endif

! Read input array
x_ptr = mxGetDoubles(prhs(1))

! Get size of array
mrows = mxGetM(prhs(1))
ncols = mxGetN(prhs(1))
size = mrows*ncols

! Create a Fortran array from the input
call mxCopyPtrToReal8(x_ptr,x_input,size)

! Prepare a matrix for output
plhs(1) = mxCreateDoubleMatrix(mrows,ncols,0)

y_ptr = mxGetDoubles(plhs(1))

call timestwo(y_output, x_input)

! copy results to output argument
call mxCopyReal8ToPtr(y_output,y_ptr,size)

return
end

subroutine timestwo(y_output, x_input)
real*8 x_input, y_output

y_output = 2.0*x_input
return
end

The output is the same if I try with the included matlabroot\extern\examples\refbook\timestwo.F fixed-form version.

My options file details (first part of output from mex -v -R2018a timestwo.F90)

Compiler location: C:\cygwin64
    Options file: C:\Users\user\AppData\Roaming\MathWorks\MATLAB\R2019a\mex_FORTRAN_win64.xml
    CMDLINE2 : C:\cygwin64\bin\x86_64-w64-mingw32-gfortran -pthread -shared -O3 -mtune=native -Wl,--version-script,"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64\fortran_exportsmexfileversion.map" C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\fortran_mexapi_version.o   -Wl,-L"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64" -llibmx -llibmex -llibmat -lm -L"C:\cygwin64\lib\gcc\x86_64-w64-mingw32\7.4.0" -lgfortran -o timestwo.mexw64
    FC : C:\cygwin64\bin\x86_64-w64-mingw32-gfortran
    DEFINES : -DMX_COMPAT_64  -DMATLAB_MEXCMD_RELEASE=R2018a  -DUSE_MEX_CMD   
    FFLAGS : -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer
    INCLUDE : -I"C:\Program Files\MATLAB\R2019a\extern\include" -I"C:\Program Files\MATLAB\R2019a\simulink\include" -I"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64"
    FOPTIMFLAGS : -O3 -mtune=native
    FDEBUGFLAGS : -g -Wall
    LDF : C:\cygwin64\bin\x86_64-w64-mingw32-gfortran
    LDFLAGS : -pthread
    LDTYPE : -shared
    LINKEXPORT : -Wl,--version-script,"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64\fexport.map"
    LINKEXPORTVER : -Wl,--version-script,"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64\fortran_exportsmexfileversion.map"
    LINKLIBS : -Wl,-L"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64" -llibmx -llibmex -llibmat -lm -L"C:\cygwin64\lib\gcc\x86_64-w64-mingw32\7.4.0" -lgfortran
    LDOPTIMFLAGS : -O3 -mtune=native
    LDDEBUGFLAGS : -g -Wall
    OBJEXT : .o
    LDEXT : .mexw64
    SETENV : set COMPILER=C:\cygwin64\bin\x86_64-w64-mingw32-gfortran
            set COMPFLAGS=-fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -DMX_COMPAT_64  -DMATLAB_MEXCMD_RELEASE=R2018a  -DUSE_MEX_CMD   
            set OPTIMFLAGS=-O3 -mtune=native
            set DEBUGFLAGS=-g -Wall
            set LINKER=C:\cygwin64\bin\x86_64-w64-mingw32-gfortran
            set LINKFLAGS=-pthread
            set LINKDEBUGFLAGS=-pthread -shared -Wl,-L"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64" -llibmx -llibmex -llibmat -lm -L"C:\cygwin64\lib\gcc\x86_64-w64-mingw32\7.4.0" -lgfortran -Wl,--version-script,"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64\fexport.map"
            set LDDEBUGFLAGS=-g -Wall
            set NAME_OUTPUT=-o "%OUTDIR%%MEX_NAME%%MEX_EXT%%"
    CYGWINROOT : C:\cygwin64
    MATLABROOT : C:\Program Files\MATLAB\R2019a
    ARCH : win64
    SRC : "C:\cygwin64\home\user\MATLAB\timestwo.F90";"C:\Program Files\MATLAB\R2019a\extern\version\fortran_mexapi_version.F"
    OBJ : C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o;C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\fortran_mexapi_version.o
    OBJS : C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\fortran_mexapi_version.o 
    SRCROOT : C:\cygwin64\home\user\MATLAB\timestwo
    DEF : C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.def
    EXP : "timestwo.exp"
    LIB : "timestwo.lib"
    EXE : timestwo.mexw64
    ILK : "timestwo.ilk"
    MANIFEST : "timestwo.mexw64.manifest"
    TEMPNAME : timestwo
    EXEDIR : 
    EXENAME : timestwo
    OPTIM : -O3 -mtune=native
    LINKOPTIM : -O3 -mtune=native
    CMDLINE1_0 : C:\cygwin64\bin\x86_64-w64-mingw32-gfortran -c -DMX_COMPAT_64  -DMATLAB_MEXCMD_RELEASE=R2018a  -DUSE_MEX_CMD    -I"C:\Program Files\MATLAB\R2019a\extern\include" -I"C:\Program Files\MATLAB\R2019a\simulink\include" -I"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64" -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -O3 -mtune=native "C:\cygwin64\home\user\MATLAB\timestwo.F90" -o C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\timestwo.o
    CMDLINE1_1 : C:\cygwin64\bin\x86_64-w64-mingw32-gfortran -c -DMX_COMPAT_64  -DMATLAB_MEXCMD_RELEASE=R2018a  -DUSE_MEX_CMD    -I"C:\Program Files\MATLAB\R2019a\extern\include" -I"C:\Program Files\MATLAB\R2019a\simulink\include" -I"C:\Program Files\MATLAB\R2019a\extern\lib\win64\mingw64" -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -O3 -mtune=native "C:\Program Files\MATLAB\R2019a\extern\version\fortran_mexapi_version.F" -o C:\Users\user\AppData\Local\Temp\mex_62691295256641_4248\fortran_mexapi_version.o

It seems like the mex-specific types like mwPointer defined in fintrf.h aren't being defined, even though I've included its location. Why are these types not recognized?

来源:https://stackoverflow.com/questions/55406709/matlab-gives-undefined-reference-errors-trying-to-use-gfortran-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!