Accessing a third party DLL from FTN95

This example (provided by mecej4 on the Silverfrost Forum) uses a driver main program compiled with FTN95 and calls a routine compiled with gFortran. This returns a vector that contains the elements of an input vector but in reverse order.

The code for the DLL, file rvec.f90:

subroutine reverse_vec(x,y,n)
implicit none
integer, intent(in) :: n
real, dimension(n), intent(in) :: x
real, dimension(n), intent(out) :: y
integer :: i

do i=1,n
  y(n+1-i) = x(i)
end do

end subroutine reverse_vec 

This is compiled into a DLL using gFortran 6.2-64bit:

gfortran -shared -o rvec.dll rvec.f90

The driver program, tvec.f90 is:

program tvec
implicit none
C_EXTERNAL reverse_vec 'reverse_vec_'
real :: x(4) = [1,2,3,4], y(4) 
call reverse_vec(x,y,4)
end program tvec

Compile and link the driver using FTN95:

ftn95 tvec.f90 /64 
slink64 tvec.obj rvec.dll /file:tvec 

The DLL is quite big. That is because it has a fixed overhead of having to contain the parts of the gFortran runtime that the DLL routine needs. Also note that the DLL does not have to be produced from Fortran code or be generated using gFortran.

Copyright © 1999-2024 Silverfrost Limited