Numerical Computing with Modern Fortran
Richard J. Hanson and Tim Hopkins
Chapter 10: Interoperability with C
These programs illustrates six examples of Fortran and C inter-operation. Fortran routines use the standard intrinsic module iso_c_binding.
Five programs may be built using the makefile provided
- dgemm_main uses dgemm_main.c, dgemm.f, xerbla.f, lsame.f, set_precision.f90
A C main program dgemm_main.c calls C_dgemm. A wrapper Fortran code c_dgemm is bound to C_dgemm and then makes a call to the Fortran Level-3 blas routine dgemm.
- saxpy_main uses saxpy_main.c, CInterSaxpy.f90, saxpy.f
A C main program saxpy_main.c calls two Fortran wrapper codes that illustrate call by reference and call by value. Both calls result in calls to the Level-1 blas code saxpy.
- syscall uses syscall.f90, csyscall.c
A Fortran routine syscall.f90 makes calls on C routines that primarily illustrate system calls. A directory is made, then made again - expecting an error - and then removing it.
- arrayOfPtrs uses arrayOfPtrs.c, PtrArray.f90
A C main program arrayOfPtrs.c calls a Fortran program and passes an array of C pointers. The Fortran routine PtrArray.f90 has the wrapper routine PtrArray bound to C PtrArray, using the same name. There is a call to the intrinsic module subprogram c_f_pointer to convert the C pointers to components of a derived type that are pointers to assumed shape, double precision arrays of rank 1. These are then printed.
- c_sam uses c_sam.f90, funptrs.c
A C main program in funptrs.c calls a Fortran program c_sam. The idea is that c_sam will make a call using function pointers to C functions contained within funptrs.c. The intrinsic module subprogram c_f_procpointer converts C function pointers to Fortran procedure pointers, allowing the calls back to the C functions. The intent is to show how a C programmer can use Fortran numerical codes that require user-written code. The C user prefers to write that code in C.
- charplay uses charplay.f90, stringplay.c
The main program example within charplay.f90 illustrates a Fortran program converting a character variable to a C string and passing that to the C routine for printing. This is done using the subprogram copy_string that is bound to C with the default C name, copy_string. The C routine that does the printing is cprint, bound to Fortran with the same name.
Next a C routine passes a string to Fortran with routine c_setString. Fortran code converts this to a character variable and prints it.
Sample output from dgemm_main
dgemm test passed
Sample output from saxpy_main
Test C_saxpy passed Test C_val_saxpy passed
Sample output from syscall
First mkdir succeeded -- OK! mkdir: cannot create directory CreatedFromFortran: File exists Second mkdir failed -- OK! rmdir succeeded -- OK!
Sample output from arrayOfPtrs
row 1 length: 3 0.100000E+01 0.200000E+01 0.300000E+01 row 2 length: 5 0.900000E+01 0.100000E+02 0.110000E+02 0.120000E+02 0.130000E+02
Sample output from c_sam
Test passed for function g Test passed for function h
Sample output from charplay
String printed from C: Hello World! Fortranized string: C set me!