Matlab crashes when calling function from fortran dll

조회 수: 4 (최근 30일)
Peter Valent
Peter Valent 2019년 12월 16일
편집: Peter Valent 2019년 12월 16일
I have a set of fortran functions/subroutines that I have compiled as DLL. I need to call some of them from Matlab. The problem is that Matlab crashes when calling functions that should return a value (other functions work). Here is a sample Fortran code of a subroutine:
SUBROUTINE SUB2(invar,outvar)
!DEC$ ATTRIBUTES DLLEXPORT :: SUB2
!DEC$ ATTRIBUTES ALIAS: 'SUB2' :: SUB2
!DEC$ ATTRIBUTES STDCALL :: SUB2
!DEC$ ATTRIBUTES REFERENCE :: outvar
REAL(8), INTENT(IN) :: invar
REAL(8), INTENT(OUT) :: outvar
outvar = invar*10.0
END SUBROUTINE SUB2
This subroutine was compiled as a DLL. As the Intel Fortran compiler does not generate header files I have created a protofile so I could lod the library into Matlab. The code of the protofile is given here:
function [methodinfo,structs,enuminfo,ThunkLibName]=myprotofile
ival={cell(1,1)};
structs=[];enuminfo=[];fcnNum=1;
fcns=struct('name',ival,'calltype',ival,'LHS',ival,'RHS',ival,'alias',ival);
ThunkLibName=[];
fcns.name{fcnNum}='SUB2';
fcns.calltype{fcnNum}='cdecl';
fcns.LHS{fcnNum}=['double'];
fcns.RHS{fcnNum}=['double'];
fcnNum=fcnNum+1;
% I dont really understand this section.
structs.c_struct.members=struct('p1', 'double', 'p2', 'int16', 'p3', 'long');
enuminfo.Enum1=struct('en1',1,'en2',2,'en4',4);
methodinfo=fcns;
I can load the library into Matlab, but as I call the function it crashes. The following functions were used:
invar = 2;
loadlibrary('SIMPLE_DLL',@myprotofile)
outvar = calllib('SIMPLE_DLL','SUB2',invar)
I have already tried many things but I am not able to find the cause. Can anyone help?

답변 (0개)

카테고리

Help CenterFile Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by