Among Fortran compilers, gfortran
can be instructed to emit C function prototypes which can be put in a header file. Here’s how to do it with gfortran (see section Options for interoperability with other languages):
$ gfortran -fc-prototypes -fsyntax-only foo.f90 > foo.h
As an example, running the command above on the file
! foo.f90
module foo
use, intrinsic :: iso_c_binding, only: c_float
implicit none
contains
real(c_float) function foo_sqr(x) bind(c)
real(c_float), intent(in) :: x
foo_sqr = x**2
end function
subroutine foo_print(x) bind(c)
real(c_float), intent(in) :: x(:)
write(*,*) x
end subroutine
end module
will produce the header file:
#include <stddef.h>
#ifdef __cplusplus
#include <complex>
#define __GFORTRAN_FLOAT_COMPLEX std::complex<float>
#define __GFORTRAN_DOUBLE_COMPLEX std::complex<double>
#define __GFORTRAN_LONG_DOUBLE_COMPLEX std::complex<long double>
extern "C" {
#else
#define __GFORTRAN_FLOAT_COMPLEX float _Complex
#define __GFORTRAN_DOUBLE_COMPLEX double _Complex
#define __GFORTRAN_LONG_DOUBLE_COMPLEX long double _Complex
#endif
void foo_print (const float *x);
float foo_sqr (const float *x);
#ifdef __cplusplus
}
#endif
For incompatible types, gfortran
will issue a friendly warning.
Question: Do any other compiler vendors support this feature?