Different number parameters calling C routine from FORTRAN 90 -
i calling c routine fortran 90 code. works fine, wondering why , how when call c routine less parameters should compiler not complain. compiler doing here? using cray compiler.
test.c
extern "c" void test_(double* x, double* y, double* z){ // work }
driver.f90
module driver ! declare arrays double precision, dimension(dim, dim), intent(in) :: x double precision, dimension(dim, dim), intent(in) :: y ! call c subroutine call test(x, y) end module driver
fortran lot different c when comes function calls. when passing arguments c routine, compiler must know type of each argument, can generate appropriate calling sequence - either put arguments on stack in correct order , correct padding or put them in expected registers. c compilers gather information when compile code if callee defined before caller. in other cases, function declaration in form of prototype should provided.
in fortran arguments typically (with exceptions) passed address, means gets passed callee set of memory pointers. pointers same - of same type , hence passed same way. therefore fortran compiler can generate function call without knowing arguments callee expecting. simplifies fortran compilers source of myriads of possible errors, calling function wrong argument types or wrong number of arguments, name few. special programs, called linters (from name of c programs verification utility lint
), have used in order guarantee no such errors present. modern fortran compilers try stricter older ones , try best detect errors whenever possible.
modern fortran versions provide interface
construct allows explicit declaration of function interfaces, similar function prototypes in c. module subroutines , functions interfaces generated automatically , made available callers use
module. when calling subroutine/function explicit interface, compiler able verify validity of call, i.e. checks if number of arguments , types matches 1 in interface.
you should provide interface external routine , compiler able perform checks. 1 uses iso_c_binding
method in order interface c code:
interface subroutine test(x, y, z) bind(c, name="test") use, intrinsic :: iso_c_binding real(kind=c_double), intent(...) :: x ! put proper intents real(kind=c_double), intent(...) :: y real(kind=c_double), intent(...) :: z end subroutine test end interface
with interface in place, call test(x, y)
result in compile-time error because of argument count mismatch.
Comments
Post a Comment