To solve the problem I basically created the DLL as a fortran dynamic linked library project, and added the two `!DEC' commands to the subroutine I wished to add to the DLL. The first (DLLEXPORT), just informs the compiler to export that routine to the DLL, the second defines the calling convention (C => cdecl) and adds an underscore to the routine name after it has been exported (the ALIAS command).
SUBROUTINE ZTEST(A,B,C,D,E,F,G) !DEC$ ATTRIBUTES DLLEXPORT :: ZTEST !DEC$ ATTRIBUTES C, ALIAS:'ZTEST_' :: ZTEST INTEGER A,B,C,D,E,F,G END
!DEC$ ATTRIBUTES C, REFERENCE, ALIAS:'ZTEST_' :: ZTESTinstead of
!DEC$ ATTRIBUTES C, ALIAS:'ZTEST_' :: ZTESTif you wish to pass vectors to the routine.
SUBROUTINE MYSUB(X,N,XMEAN) CDEC$ ATTRIBUTES DLLEXPORT,C,REFERENCE,ALIAS:'mysub_' :: MYSUB IMPLICIT DOUBLE PRECISION (A-H,O-Z) DOUBLE PRECISION X(N) XMEAN=0D0 DO 10 J=1,N XMEAN=XMEAN+X(J) 10 CONTINUE XMEAN=XMEAN/N RETURN ENDThe ALIAS:'mysub_' is needed because R expects that a routine name exported from a DLL will have an underscore appended to it, but CVF does not do this by default.
df rtest.f /dllThis will create a file rtest.dll file that should be kept, and .obj, .lib and .exp files that are not needed for R and can be deleted.
> dyn.load("E:\\rtest.dll")
> is.loaded(symbol.For("mysub"))
[1] TRUE
> x<-1:6
> .Fortran("mysub",as.double(x),as.integer(length(x)),xmean=double(1))
[[1]]
[1] 1 2 3 4 5 6
[[2]]
[1] 6
$xmean
[1] 3.5
>
Note: Fortran 90 constructions can be used within the routine, e.g.
the MYSUB routine could be written as
Subroutine mysub(x,n,xmean) !DEC$ ATTRIBUTES DLLEXPORT,C,REFERENCE,ALIAS:'mysub_' :: MYSUB Implicit Double Precision (a-h,o-z) Double Precision x(n) xmean=Sum(x)/n Return End... but Fortran 90 constructions involving the arguments will not work. E.g. this version will cause R (ver. 1.5.1) to crash:
Subroutine mysub(x,xmean) !DEC$ ATTRIBUTES DLLEXPORT,C,REFERENCE,ALIAS:'mysub_' :: MYSUB Implicit Double Precision (a-h,o-z) Double Precision x(:) xmean=Sum(x)/Size(x) Return End
Last modified: $Date: 2004-12-07 14:24:26 -0500 (Tue, 07 Dec 2004) $, by Duncan Murdoch