Topic: FORTRAN dll - Undefined reference

I am trying to build a FORTRAN dll which will be used with a C# interface. The dll project contains multiple C++ dlls and one FORTRAN static library (.a). When I build the project I get "Undefined reference to" error for all the routines included in the FORTRAN static library. I also have an equivalent EXE version where the whole project is built as a standalone executable. The EXE is built and linked very well and I can also use that with a C# interface.

I am lost on to what is happening in the dll mode and why is there a Undefined reference error. I am using Simply Fortran 1.45.

Thank you for your help.

Regards
Sankar

Regards
skp

Re: FORTRAN dll - Undefined reference

Sankar,

Building DLLs require considerably more details to be properly linked with C code.  Are you working with 32- or 64-bit projects?

If 32-bit, you may run into complications due to "name mangling" by the Fortran compiler and assumed mangling by the C++ or C# compiler (I'm not clear on what is calling what from your description).  In 32-bit mode, you need to declare all exported functions properly.  There is another post about this issue at: http://forums.approximatrix.com/viewtopic.php?id=380.

Using a Fortran static library to be linked in to the DLL is a bit unorthodox, but it should work.  However, the declarations that are outlined in the link above would still be necessary in your Fortran code.  When compiling your Fortran code, you may also need to add a flag to the Fortran Compiler flags:

-fno-underscoring

to make the function "decorations" in the object file conform to what C, C++, or C# would expect.

Let me know if the above gets you started.  Some additional details about what project is calling what would be helpful.

Jeff Armstrong
Approximatrix, LLC

Re: FORTRAN dll - Undefined reference

Jeff,

Thank you for your suggestions. I now have it all started but it is not all trouble free. :-). Here is my basic setup:

- C# calling a FORTRAN DLL
- FORTRAN DLL has a few subroutines which are exported. In the FORTRAN DLL one of the routines calls an external C++ DLL to perform some calculations.

With this setup, when I am running the whole code, the routines that does not have the C++ DLL interface works all the time returning correct values to C#. However, the routine with the C++ interface returned the right value the first time. After that the C# call to that routine is always crashing with the "AccessViolationException is Unhandled" error with following message:

"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

I am unable to attach files to this otherwise I want to send you the whole code and the C++ dll so that you can check the issue.

Let me know if you have any suggestions.

Thanks
Sankar

Regards
skp

Re: FORTRAN dll - Undefined reference

If I understand correctly, basically something goes wrong when the Fortran routine in a Fortran-only DLL calls a C++ routine in a C++-only DLL.  Is that correct?

So I have a few questions.   Did you compile the C++ DLL with Simply Fortran's compiler or Microsoft's (or another)?  If it was another compiler, do you know what calling standard it is using?  Is it also explicitly declared to be STDCALL?

If so, does your Fortran code declare a proper INTERFACE block for the C++ routine?  The Fortran calling routine would also need to know that the routine it is calling is a) a C-compatible routine and b) a procedure using STDCALL.  If either isn't true, it could work but corrupt memory in the process. 

Also, what type of variables are you passing?  The Fortran declarations must be done properly in order to ensure reliability.  For example, if your C routine is expecting two integers, you must have an appropriate INTERFACE in your Fortran code:

INTERFACE
   FUNCTION TWOINT(X, Y) BIND(C)
   USE ISO_C_BINDING
      INTEGER(KIND=C_INT)::TWOINT
      INTEGER(KIND=C_INT), VALUE::X,Y
   END FUNCTION TWOINT
END INTERFACE

The "VALUE" modifier is necessary because Fortran will, by default, pass by reference, whereas C expects passing by value.

Jeff Armstrong
Approximatrix, LLC