For these compilers, some C functions can be called without using the C interop features of Fortran 2003. For example, the C code
#include <stdlib.h>
void sum_abs_int_(int *in, int *num, int *out) {
int i,sum;
sum = 0;
for (i=0; i < *num; ++i) {sum += abs(in[i]);}
*out = sum;
return;
}
void sum_abs_int_val_(int *in, int num, int *out) {
int i,sum;
sum = 0;
for (i=0; i < num; ++i) {sum += abs(in[i]);}
*out = sum;
return;
}
void sum_abs_double_(double *in, int *num, double *out) {
int i;
double sum;
sum = 0;
for (i=0; i < *num; ++i) {sum += abs(in[i]);}
*out = sum;
return;
}
double twice_(double *x) {
return *x*2;
}
double thrice(double *x) {
return *x * 3;
}
can be called from
program xcallc
implicit none
integer, parameter :: n = 3, ivec(n)=[4,-2,7], dp = kind(1.0d0)
integer :: isum
real(kind=dp) :: dsum,twice
write (*,"(a,3(1x,i0))") "ivec =",ivec
call sum_abs_int(ivec,n,isum)
print*,"sum(abs(ivec))=",isum
call sum_abs_int_val(ivec,%val(n),isum) ! use %val() extension to pass by value
print*,"sum(abs(ivec))=",isum
call sum_abs_double(dble(ivec),n,dsum)
print*,"sum(abs(dble(ivec)))=",dsum
print*,twice(5.3d0)
end program xcallc
with the compilation script
gfortran -c xcallc.f90
gcc -c cforf.c
gfortran cforf.o xcallc.o
if exist a.exe a.exe
giving
ivec = 4 -2 7
sum(abs(ivec))= 13
sum(abs(ivec))= 13
sum(abs(dble(ivec)))= 13.000000000000000
10.600000000000000
and the C function thrice()
that does not have an underscore can be called from Fortran code that is compiled with the gfortran -fno-underscoring option. Admittedly the Fortran code is nonstandard since it uses the %val() function, and the above procedure may not work for other pairs of compilers.