I want to compile and link a dynamic link library (dll) from a Fortran code using gfortran (mingw - 64bit) in windows. I test generated dlls in Excel 64-bit. I noticed, Excel does not return any result (it returns: #VALUE!) for the below Fortran code:
Function RANDGF() bind(c)
use ISO_C_BINDING
implicit none
!GCC$ ATTRIBUTES DLLEXPORT :: RANDGF
Double Precision :: RANDGF
Double Precision :: A
Call RANDOM_NUMBER(A)
RANDGF = 2.0
END
However, if I comment the Call RANDOM_NUMBER(A) line, the DLL returns the expected result.
The command that I use for the compilation is:
gfortran -shared -fpic -o randgf.dll randgf.f90
Here is my VBA Code in Excel:
Private Declare PtrSafe Function randgf Lib "randgf.dll" () As Double
Public Function gfort_res() As Double
gfort_res = randgf()
End Function
What should I do in order to call functions like RANDOM_NUMBER in my code, if I want to use them in a DLL?