Topic: making dll to use in R

Hello,
I need to call Fortran subroutines into a 64-bit R through dll files. My main goal is to share my research-codes through dll files, so that practitioners can use them. To create a 64-bit dll, I am currently using the SimplyFortran trial version. I have created the dll in SimplyFortran, but the dll doesn’t work in R. I am very new to SimplyFortran and not sure if I am making any mistakes. Here is a (f77) sample code that I am trying to use in R:

      subroutine add(sm,a,b)
      implicit none
      integer sm, a, b
      sm = a + b
      return
      end

To create the dll in SimplyFortran, I save and open the ‘An empty static library project’ and follow the steps:
(1)    Click on Project > Add files(s)… > choose the subroutine add.f90
(2)    Project > Options… > Target Name: add.dll
(3)    Build > Build now!

Here is how I load the dll in R:
dyn.load("C:/Users/Sam27/Desktop/projects/add.dll")

Here is the error msg:
Error in .C("add_", as.integer(-999), as.integer(10), as.integer(15)) :
  C symbol name "add_" not in load table

I see that similar problems have been discussed, but I have not been able to solve mine. Would you please give me helpful comments? I will really appreciate it.

Best regards,
Sam

Re: making dll to use in R

Sam,

One thing  I noticed from your description is that you didn't mention changing the architecture to 64-bit.  If you're attempting to access the DLL from 64-bit R, the DLL will need to be 64-bit as well.

You can also see what names are exported in the DLL you've compiled by opening it (just by selecting Open in the File menu) in Simply Fortran.  You should be presented with a list of symbols, and you can search for "add_" to make sure it's available and named properly.

Jeff Armstrong
Approximatrix, LLC

Re: making dll to use in R

Hi Jeff,
Thanks for your reply; and I am sorry for my delayed response. Unfortunately, my problem has not been solved yet.

I forgot to mention that I am changing the architecture correctly from the (default) 32-bit to 64-bit, when I am using a 64-bit R.

I think your second comment addresses my problem. I think the DLL is not created properly. When I open the 64-bit DLL, that is, add.dll, from file menu (File > Open > add.dll), I don't find 'add_' among the bunch of symbols, such as, 6c3ca3a8 b .bss.

Am I making any mistake? Do you have any thoughts on this? Or, is it because I am using a trial version of SimplyFortran?

Best regards,
Sam

Re: making dll to use in R

It looks like its just a name mangling error where the name in the DLL doesn't match what you have in the Fortran code.

I am not familiar with R's requirements.

Try changing your code to the following to ensure the dll contains a subroutine actually called "add". You need to be compiling for Fortran 2003 for BIND to be accepted.

      subroutine add(sm,a,b) bind(C, name="add")
      implicit none
      integer sm, a, b
      sm = a + b
      return
      end

Also should your load command be:

dyn.load("C:\Users\Sam27\Desktop\projects\add.dll")

or even

dyn.load("C:\\Users\\Sam27\\Desktop\\projects\\add.dll")

?

--
David

Re: making dll to use in R

Hi David,
The problem is solved and everything is working now. I have been using the following R commands to call DLLs into R that are created using free compilers (32-bit MinGW, gfortran, gcc): 
        dyn.load("C:/Users/Sam27/Desktop/projects/add.dll")
        .C("add_", as.integer(-999), as.integer(10), as.integer(15)) :

However, I needed to modify the R code slightly to call the DLLs created using SimplyFortran; here is the corrected code:
        dyn.load("C:/Users/Sam27/Desktop/projects/add.dll")
        .C("add", as.integer(-999), as.integer(10), as.integer(15)) :

Your suggestions helped me try it differently. Thank you very much. 

Best regards,
Sam