问题
I am trying to use Fortran with C++. I created a DLL(C++) by following this guide. However, instead of creating C++ MathClient.cpp
program, which calls this DLL (as it is described in the guide), I created Fortran MathClient.F90
program, which calls this DLL.
Fortran main code:
PROGRAM MAIN
USE MOD_INTERFACE
IMPLICIT NONE
INTEGER(C_INT) :: A, B
INTEGER(C_INT) :: INDEX
INTEGER(C_INT) :: CURRENT
LOGICAL(C_BOOL) :: BOLEAN
A = 1
B = 1
! Initialize a Fibonacci relation sequence.
CALL FIBONACCI_INIT(A, B)
! Write out the sequence values until overflow.
DO
BOLEAN = FIBONACCI_NEXT()
IF (.NOT. BOLEAN) EXIT
INDEX = FIBONACCI_INDEX()
CURRENT = FIBONACCI_CURRENT()
WRITE(*,*) INDEX, ': ', CURRENT
END DO
WRITE(*,*) FIBONACCI_INDEX() + 1, &
'Fibonacci sequence values fit in an unsigned 64-bit integer.'
END PROGRAM MAIN
Fortran module code:
MODULE MOD_INTERFACE
USE, INTRINSIC :: ISO_C_BINDING
INTERFACE
SUBROUTINE FIBONACCI_INIT ( A, B ) bind ( C, name = "fibonacci_init" )
IMPORT
IMPLICIT NONE
INTEGER (C_INT) :: A, B
END SUBROUTINE FIBONACCI_INIT
LOGICAL(C_BOOL) FUNCTION FIBONACCI_NEXT ( ) bind ( C, name = "fibonacci_next" )
IMPORT
IMPLICIT NONE
END FUNCTION FIBONACCI_NEXT
INTEGER(C_INT) FUNCTION FIBONACCI_CURRENT ( ) bind ( C, name = "fibonacci_current" )
IMPORT
IMPLICIT NONE
END FUNCTION FIBONACCI_CURRENT
INTEGER(C_INT) FUNCTION FIBONACCI_INDEX ( ) bind ( C, name = "fibonacci_index" )
IMPORT
IMPLICIT NONE
END FUNCTION FIBONACCI_INDEX
END INTERFACE
END MODULE MOD_INTERFACE
The C++ functions in MathLibrary.cpp
are the same as in the above-mentioned guide. The only difference is, that I add some std::cout
, in order to print values of received variables in C++ functions.
The problem is, that C++ functions receive wrong integer(C_INT
) values from Fortran main code. For example, the void fibonacci_init
function should receive values current_ = 1, previous_ = 1
during initialization. Instead of that, the values are some huge integer numbers (current_ = 45028402782009632
).
I am using Intel Parallel Studio and Visual Studio. I will be grateful for any help.
来源:https://stackoverflow.com/questions/65553923/fortran-c-binding-wrong-value-being-read-in-dllc