Calling across the boundary
A project rarely chooses one language for everything: a numerical core in Fortran and the rest of the program in C++ is a common shape, and the place where the two meet is an interface with no type information on one side and no name mangling on the other. This page builds both directions with this image's compilers and shows the whole command sequence, because the commands are the interface.
C++ calling Fortran
The Fortran side is a subroutine with BIND(C, NAME="stats"): the attribute makes the symbol name exactly stats with no compiler prefix, and it restricts the arguments to the types in ISO_C_BINDING. The array arrives as a pointer and the length as a value, which is how C passes an array; a Fortran dummy without VALUE is a pointer on the C side.
The file stats.f90:
subroutine stats(x, n, mean, sd) bind(c, name="stats")
use, intrinsic :: iso_c_binding
implicit none
integer(c_int), value :: n
real(c_double), intent(in) :: x(n)
real(c_double), intent(out) :: mean, sd
integer :: i
mean = 0.0_c_double
do i = 1, n
mean = mean + x(i)
end do
mean = mean / real(n, c_double)
sd = 0.0_c_double
do i = 1, n
sd = sd + (x(i) - mean)**2
end do
sd = sqrt(sd / real(n - 1, c_double))
end subroutine stats
The file main.cpp declares the same function with extern "C" and calls it:
#include <cstdio>
extern "C" void stats(const double* x, int n, double* mean, double* sd);
int main() {
const double v[4] = {2.0, 4.0, 4.0, 5.0};
double mean = 0.0;
double sd = 0.0;
stats(v, 4, &mean, &sd);
std::printf("C++ main: mean = %6.3f sd = %6.3f\n", mean, sd);
return 0;
}
gfortran -Wall -Wextra -std=f2018 -c stats.f90
g++ -std=c++23 -Wall -Wextra main.cpp stats.o -o cpp_main
./cpp_main
C++ main: mean = 3.750 sd = 1.258
The numbers are the ones the module page produced in Fortran alone, which is the point: the arithmetic did not change, only the caller did. The C++ file has no header for the Fortran subroutine, so the declaration is a promise the linker checks against the symbol name and nothing else.
Fortran calling C++
The other direction needs extern "C" on the C++ side so that the symbol is not mangled, and an INTERFACE block with BIND(C) on the Fortran side so that the compiler knows the calling convention and the types.
The file hyp.cpp:
#include <cmath>
extern "C" double hyp(double a, double b) {
return std::sqrt(a * a + b * b);
}
The file use_hyp.f90:
program use_hyp
use, intrinsic :: iso_c_binding
implicit none
interface
function hyp(a, b) bind(c, name="hyp") result(y)
import :: c_double
real(c_double), value :: a, b
real(c_double) :: y
end function hyp
end interface
print '(a,f6.3)', 'fortran main: hyp(3,4) = ', hyp(3.0_c_double, 4.0_c_double)
end program use_hyp
g++ -std=c++23 -Wall -Wextra -c hyp.cpp
gfortran -Wall -Wextra -std=f2018 use_hyp.f90 hyp.o -o f_main
./f_main
fortran main: hyp(3,4) = 5.000
The rules of the boundary
Three rules cover almost every mistake. The symbol name must not be mangled: a C++ function without extern "C" is called _Z3hypdd in the object file, and the Fortran linker looks for hyp; the error is an undefined reference to the plain name. The types must be the C ones: c_double, c_int, c_float, c_char and c_ptr, not real(8) and integer, because the size of the default integer is not part of either language's promise. Arguments pass by reference unless the Fortran dummy says VALUE: a C scalar argument maps to a Fortran dummy with VALUE, an array maps to a plain dummy, and a string is a char* against a character(kind=c_char) with the length passed separately, because C strings are null-terminated and Fortran strings are not.
What the boundary forbids
Everything that makes Fortran pleasant inside the language stops at this boundary. An allocatable array is not interoperable: it is a descriptor, and the descriptor is compiler-specific, so a C++ function cannot allocate a Fortran array or ask for its bounds. A derived type is interoperable only with BIND(C) and only if every component is, which rules out allocatable and pointer components. A module procedure is not reachable by name unless it also has BIND(C), and optional arguments do not exist on the C side. The practical answer is the one this page shows: the boundary is a thin layer of plain functions with double arrays and lengths, and everything else stays on one side of it.
The same boundary is what makes a Fortran library callable from Python (lesson 107), from Julia or from C, which is the larger argument for keeping it thin and boring.
Exercises
- Remove extern "C" from hyp.cpp and build again. What does the linker say, and what is the mangled name?
- Change the Fortran VALUE on n to a plain dummy and rerun. What does the program compute, and why does it not fail?
- Add a third argument to stats on the Fortran side and not on the C++ side. Which compiler notices, and when?
- Pass a std::vector<double> to stats with v.data(). Does anything change in either source?
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and G++ (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container. The examples are two files each, which the course checker cannot join from one block; the transcripts above are the builds that were run.
Article author: Arthur Isaev