I have some C code that I compile to a dll object.
I have a separate project, written in Fortran, which I too compile to a single dll object.
I need to call a function inside the Fortran dll from the C dll, without using LoadLibrary. I also need the 2 dll to remain separate, so I can replace one or the other without re-compiling them together.
I have read some information on the topic, but I can't get it to work. The compiler does not throw errors or warnings, but when I start my main program it fails. Here's what I tried:
I have declared all functions I wish to call from the C code with
BIND ("C", NAME="FUNCNAME")
I have declared the
USE, INTRINSIC :: ISO_C_Binding
inside the functions and subroutines I intend to call from the C codeI included
!DEC$ ATTRIBUTES DLLEXPORT :: NAME
inside the Fortran code (I am using ifort)I use c type variables in my Fortran code
I pass variables by reference in the C code
I included the Fortran dll .lib file as a source file to the C project
According to most sources and docs I consulted, I should be able to simply do the following:
In Fortran:
SUBROUTINE FORTRANDLLFUNCTION(arg1, arg2, arg3, char1) BIND (C, NAME='FORTRANDLLFUNCTION')
USE, INTRINSIC :: ISO_C_Binding
IMPLICIT NONE
!DEC$ ATTRIBUTES DLLEXPORT :: FORTRANDLLFUNCTION
REAL(C_FLOAT), INTENT(INOUT) :: arg1
REAL(C_FLOAT), INTENT(INOUT) :: arg2
REAL(C_FLOAT), INTENT(INOUT) :: arg3
CHARACTER(KIND=C_CHAR), INTENT(IN ) :: char1
<do stuff>
END SUBROUTINE FORTRANDLLFUNCTION
In C:
extern void FORTRANDLLFUNCTION(float**, float**, float**, char*, int);
float a, b, c;
char abc;
FORTRANDLLFUNCTION(&a, &b, &c, abc, sizeof(abc));
However, I get an error when running the main program. If I remove the call to the Fortran function and run the main program with the compiled C dll, it works. What could be the source of my problem?
This is the output of dumpbin /exports
on the compiled Fortran dll:
And this is the result of dumpbin /imports
on the compiled C dll: