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 Center および File ExchangeFortran with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by