Matlab crashes when calling function from fortran dll

3 visualizaciones (últimos 30 días)
Peter Valent
Peter Valent el 16 de Dic. de 2019
Editada: Peter Valent el 16 de Dic. de 2019
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?

Respuestas (0)

Categorías

Más información sobre Fortran with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by