FortWrap version 2.0 released 4/26/2015
FortWrap is a python script that parses Fortran 90/95/200X source files and generates wrapper code for interfacing with the original Fortran code from C++. FortWrap is compatible with both g95 and gfortran and is intended to be used with Fortran code that takes an object oriented approach and makes use of Fortran derived types. The resulting wrapper code provides a C++ interface that wraps the Fortran derived types with C++ proxy classes.
For example, consider the following Fortran code:
MODULE m TYPE Object ... END TYPE Object CONTAINS SUBROUTINE object_ctor(o, n, x) TYPE (Object) :: o INTEGER, INTENT(in) :: n REAL, INTENT(in) :: x(n) ... END SUBROUTINE object_ctor SUBROUTINE foo(o) TYPE (Object) :: o ... END SUBROUTINE foo END MODULE m
After wrapping with FortWrap, these routines can be utilized in a natural way from C++:
std::vector<float> x(10); ... // Define values for x Object o(&x); // C++ constructor: automatically calls object_ctor o.foo();
Additional examples can be found here
FortWrap can also be used as an intermediary step for wrapping Fortran code with Swig. The C++ header files generated by FortWrap can be directly processed by Swig using the -c++ option. This allows you to take full advantage of Swig's powerful capabilities for wrapping C++ code. For example, FortWrap can be used with Swig to create an object oriented interface in python. This works particularly well with Swig's -keyword argument for handling Fortran optional arguments.
Many features of Fortran 90/95/2003 are not supported by FortWrap. In some cases (e.g. assumed shape arrays) this is because the Fortran language standard does not provide an interoperability mechanism.
In most situations, it is possible to get FortWrap to wrap these types of routines by providing a Fortran wrapper that is interoperable and calls the target routine.
The following argument types/constructs/features are not supported:
Note that FortWrap can still wrap procedures that use unsupported arguments if those arguments are optional. In these cases, the offending arguments are hidden from the generated interface code.
Fortran 2003 provides support for polymorphism and Type Bound Procedures, which behave more like traditional class methods. A good introduction to these language features is here.
FortWrap version 2 introduced experimental support for wrapping these features for the gfortran compiler. The objective is to mirror the Fortran class inheritance structure using C++ classes. Please see the examples included with FortWrap in the tests/classes directory. Many features are currently working, including wrapping type bound procedures and mirroring of the inheritance structure. Supporting these features requires more assumptions about how the Fortran compiler generates code, so they may be considered more fragile.