Topic: Fortran from Excel

Your blogs 4 and 5 describe how to call Fortran from Excel via Visual Basic. To me, a novice, there seem to be gaps in these. These are old blogs However, I’ve got to these routines to average to numbers, which compile ok.

MEAN.FOR as follows:

Module MEAN_MODULE
    USE ISO_C_BINDING
    IMPLICIT NONE
CONTAINS
   FUNCTION MEAN(A,B) BIND(C, name=“mean”) RESULT (M)
      REAL(C_DOUBLE), VALUE :: A, B
      REAL(C_DOUBLE) M
      M = (A + B)/2.
   END FUNCTION
END MODULE


MEAN.DEF as follows:

LIBRARY MEAN
EXPORTS
   MEAN=“MEAN@16”

This gets … Generating Makefile Okay
But then ….  ld returned 1 exit status
               Last command making (target.exe) returned in bad status.

Any clue as to what I am missing, please.

Re: Fortran from Excel

I can see a few problems in your post.  First, your function that you want to have callable from a DLL is inside a Fortran module.  Using a module to encapsulate DLL-exported functions really won't work well since the compiler is not generating a simple name for the function.  You want to move the function outside the module so it is standalone:

   FUNCTION MEAN(A,B) BIND(C, name=“mean”) RESULT (M)
      REAL(C_DOUBLE), VALUE :: A, B
      REAL(C_DOUBLE) M
      M = (A + B)/2.
   END FUNCTION

Second, the blog's instructions were for generating a 32-bit DLL.  There's a good chance that, hopefully, you're using 64-bit Excel, which simplifies everything greatly.  Specifically, the export definition where it adds the "standard call" decoration of @16 is no longer necessary.  You probably don't need to create an export definition file at all if you're in 64-bit mode.

Third, when you build your library, you'll notice that the compiler is attempting to make an executable named "target.exe."  It's almost certainly failing because your code does not contain any program unit.  You'll need to ensure you're doing two things:

1. Use a Project - You need to create a new project (Shared Library (DLL)) by selecting "New Project" in the Project menu.  After you set up and select a reasonable location for your project, possibly the same directory where your Fortran source code is, you need to add your Fortran source code to the project (again, in the Project menu, select "Add File(s)") and save the project.

2. Configure the Project - In Project Options, make sure that the Architecture is selected to match your Excel installation's architecture (32- or 64-bit) and the project type is set to "Shared Library."  Save your project after you make any changes.

After you build a DLL, you should be able to access it via Excel's Visual Basic for Applications interface.  However, accessing a DLL from Excel isn't a simple process.  It is actually quite complicated.  You'll need to make sure the Excel VBA code has the correct path to the DLL and that the function's signature is correct.  The process gets harder once you start passing arrays to Fortran.  It is completely possible, but there is a learning curve here that is significant.  I just wanted to warn you.

Jeff Armstrong
Approximatrix, LLC

Re: Fortran from Excel

Many  thanks, Jeff
It helped bundle.
The learning curve was indeed significant, steep, and long.
Roger

Re: Fortran from Excel

Re: Fortran from Excel

Jeff, Indeed there was a great deal to learn to link these. I now have it all running sweetly.

But ... How can I WRITE or PRINT from my Fortran DLL called from Excel, as this causes a disconnect.
AI tells me I should obtain SimplyFortran's files libgfortran-5.dll, and libgcc_s_dw2-1.dll and libqualmath-0.dll
I cannot find these. Can you please somehow get me over this hurdle.. Thank you

Re: Fortran from Excel

Roger,

You won't be able to use WRITE or PRINT to standard output because there is no "standard output" available.  You can write to a file if you first use an OPEN statement, of course. 

When your Fortran code is called by Excel as a DLL, it is running within the Excel process.  Excel, though, is a true Windows program; it is not running with access to "standard output," usually a terminal or command prompt window in Windows.  When Fortran tries to print to "standard output" while running inside Excel, it has nowhere to send its text.

You would really need to either print to a file after you open it with an OPEN call or somehow send strings to Excel instead.

AI was telling you to copy the Fortran runtime library DLLs, which, in Simply Fortran's default setup, aren't even used (the runtime is statically linked within the DLL or executable).  The AI answer is nonsensical and has no bearing on your particular situation.

Jeff Armstrong
Approximatrix, LLC