Calling Fortran from C++¶
Learn Fortran · Full GaspardOS API from Fortran
The GaspardOS x64 toolchain provides GCC 4.6.4, C++ and Fortran compilers,
and static libstdc++ and libgfortran libraries. This guide calls a
Fortran numerical routine from C++98 through a shared C interface.
Interface and types¶
Use bind(C, name="...") on the Fortran routine and extern "C" on its
C++ declaration to select the symbol name and avoid C++ name mangling.
Choose interoperable types from iso_c_binding:
| Fortran | C++ | Passing convention |
|---|---|---|
integer(c_int), value |
int |
By value |
real(c_double), value |
double |
By value |
real(c_double) |
double* |
Scalar address |
real(c_double) :: x(*) |
double* |
First element address |
Without value, an interoperable scalar is passed by address. intent(in)
and intent(inout) describe data usage, not the passing convention.
Complete example¶
Save this routine as scale.f90. It multiplies each element by a factor;
the C++ program owns the array.
subroutine scale_values(n, factor, values) bind(C, name="scale_values")
use iso_c_binding, only: c_int, c_double
implicit none
integer(c_int), value, intent(in) :: n
real(c_double), value, intent(in) :: factor
real(c_double), intent(inout) :: values(*)
integer(c_int) :: i
do i = 1, n
values(i) = factor * values(i)
end do
end subroutine scale_values
Save the caller as main.cpp:
#include <cstdio>
extern "C" void scale_values(int n, double factor, double* values);
int main()
{
double values[] = {1.0, 2.0, 3.0};
scale_values(3, 2.0, values);
std::printf("%.1f %.1f %.1f\n", values[0], values[1], values[2]);
return 0;
}
Expected output on GaspardOS: 2.0 4.0 6.0.
Build from Windows / MSYS2¶
After building the toolchain and runtime libraries, run these commands from the repository root in MSYS2 MINGW64, with both files at the root:
export PATH="$PWD/build-x64-install/bin:$PATH"
x86_64-gaspardos-gfortran -Wall -Wextra -c scale.f90 -o scale.o
x86_64-gaspardos-g++ -std=gnu++98 -Wall -Wextra -c main.cpp -o main.o
x86_64-gaspardos-g++ main.o scale.o -lgfortran -lm -o cpp_fortran.elf
The C++ driver adds its standard library; -lgfortran adds the Fortran
runtime and -lm adds mathematical functions. Put objects before libraries
for static linking. Both compilers must use the same target and SDK.
Copy the resulting GaspardOS ELF64 executable to the target system;
it is not a Windows executable.
Arrays and limitations¶
- C++ normally uses zero-based indexing; Fortran
values(1)in this example refers to C++values[0]. - Fortran stores matrices in column-major order. For a Fortran array
a(rows, cols),a(i, j)maps to C++ buffer offset(i - 1) + rows * (j - 1). - Supply contiguous storage with at least
nvalid elements and keep it alive during the call. This routine does not check the buffer size. - With GCC 4.6.4, use explicit-size or assumed-size
(*)arrays for this C interface; assumed-shape(:)andallocatablearguments do not fit this interface. - Do not pass
std::string,std::vector, C++ objects, exceptions or Fortran strings with implicit lengths directly across this boundary. Expose scalars, buffers and explicit lengths instead.
The port disables OpenMP, threads, shared libraries and libquadmath.
This example only performs numerical computation; Fortran I/O also depends
on runtime support and the newlib services available on the target.