0

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?

khajlk
  • 791
  • 1
  • 12
  • 32
  • 1
    This is related to [here](https://stackoverflow.com/q/50477689/3157076)? – francescalus May 29 '18 at 19:26
  • Did you mean that I should use -cpp flag? like this: gfortran -shared -o test_mod.dll test_mod.f90 -cpp – khajlk May 29 '18 at 19:33
  • I tried. At least, it compiled without errors and warnings. But, result is not as expected (i.e. not a square of the first number). Can you reproduce the problem? – khajlk May 29 '18 at 19:34

1 Answers1

0

Okay, I figured out the thing. I fixed the problem and the full credits goes to the comment of @francescalus above :).

The following line at Windows CMD created DLL without errors and warnings and I am able to read and execute Fortran subroutine in R.

gfortran -shared -o test_mod.dll test_mod.f90 -DCOMPILER_GFORTRAN -cpp

Based on @Vladimir's comment, I would like to add that if code file is renamed to .F90. Then, it can also be compiled without -cpp flag, like follows:

 gfortran -shared -o test_mod.dll test_mod.F90 -DCOMPILER_GFORTRAN
khajlk
  • 791
  • 1
  • 12
  • 32
  • Or name the file with capital `F`, but I am not sure if that also works on windows, but it might. I thought there will be some duplicate to close this questions is, but I can't find a good one. – Vladimir F Героям слава May 30 '18 at 09:00
  • So, naming the file with .F90 and compiling it gfortran -shared -o test_mod.dll test_mod.f90 -DCOMPILER_GFORTRAN. This is what you meant? – khajlk May 30 '18 at 09:08
  • 1
    Yes, with capital F gfortran calls `-cpp` automatically. At least on Linux. But I personally use `.f90` and `-cpp` as you showed above. – Vladimir F Героям слава May 30 '18 at 09:27
  • Perfect! You are cool! I can confirm that you are right. It also worked perfectly fine (on Windows10) after renaming to .F90 and without using -cpp flag. So, my question is: which one is preferable? Is it a matter of choice only ? – khajlk May 30 '18 at 09:38
  • I prefer the "explicit is better than implicit" philosophy, so I'd go for the -cpp route. – Rodrigo Rodrigues May 30 '18 at 13:44