On my Windonws 10 (x64) machine, I have been trying to call Fortran
subroutines in R
using .Fortran()
function via gfortran
. The following example code (test.f90
) works fine:
The Example code:
! Computes the square of a number
Subroutine sr1(a,b)
!DEC$ ATTRIBUTES DLLEXPORT::sr1
!DEC$ ATTRIBUTES C, REFERENCE, ALIAS:'sr1' :: sr1
implicit none
integer a,b
b = a*a
End Subroutine sr1
I compiled code in gfortran
, which worked fine:
gfortran -shared -o test.dll test.f90
and then calling this subroutine in R
:
dyn.load("path_to_file/test.dll")
is.loaded("sr1") #Returns TRUE
.Fortran("sr1", a=as.integer(12), b=as.integer(10))
What I would like to do:
I also have Intel Fortran (iFORT
) and Lahay Fortran
compilers installed at my machine. Now, I would like to add pre-processor directives in the above code for these multiple Fortran compilers (so that same test.f90
file can be used for all compilers).
What I have tried:
I found a relevant question here, and tried to modify the code (test_mod.f90
) like follows:
! Computes the square of a number
Subroutine sr1(a,b)
#ifdef COMPILER_GF
!DEC$ ATTRIBUTES DLLEXPORT::sr1
!DEC$ ATTRIBUTES C, REFERENCE, ALIAS:'sr1' :: sr1
#endif
#ifdef COMPILER_IF
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL ::sr1
!DEC$ ATTRIBUTES DECORATE, ALIAS : 'sr1' :: sr1
!DEC$ ATTRIBUTES REFERENCE :: a,b
#endif
#ifdef COMPILER_LF
dll_export sr1
#endif
implicit none
integer a,b
b = a*a
End Subroutine sr1
I tried to compile the code using:
gfortran -shared -o test_mod.dll test_mod.f90 -DCOMPILER_GF
and got this errors:
Warning: test_mod.f90:5: Illegal preprocessor directive
Warning: test_mod.f90:8: Illegal preprocessor directive
Warning: test_mod.f90:10: Illegal preprocessor directive
Warning: test_mod.f90:14: Illegal preprocessor directive
Warning: test_mod.f90:16: Illegal preprocessor directive
Warning: test_mod.f90:18: Illegal preprocessor directive
test_mod.f90:17.1:
dll_export sr1
1
Error: Unclassifiable statement at (1)
I am new to Fortran and most probably, made a mess in compilation or adding pre-processor directives in incorrect way. Can someone suggest me how can I fix this issue?