Simple Examples for calling C/C++ from Fortran

Andrew J. Pounds, Ph.D
Departments of Chemistry and Computer Science
Mercer University

There are several web sites that discuss how one might call C from Fortran, but most of these are either for commercial compilers or are devoid of simple examples of how one might call routines with arguments. The following examples are bare-bones calls to demonstrate how to use create FUNCTIONS and SUBROUTINES in C/C++ that can be called from Fortran using the Gnu compilers.


driver.f (Fortran code that calls C)
testret.cc (void function to modify an argument)
testdot.cc (float function to return a value)
testmat.cc (void function to modify an array argument)
makefile (compiles everything and also builds a library)

Remember that Fortran passes by reference -- so the C functions have to use pointers to variables!