FortWrap

FortWrap version 1.0 released 5/27/2013

Introduction

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

Wrapping for other languages

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.

Obtaining FortWrap

FortWrap may be downloaded from https://sourceforge.net/projects/fortwrap/.

Main Features

Unsupported Features

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 polymorphism wrapping status

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.

I am currently working on developing support for these features for use with gfortran. The objective is to mirror the Fortran class inheritance structure using C++ classes. I believe that I have identified most of the basic patterns that are needed to make this wrapping work. I am currently working to incorporate this into FortWrap, and expect to have a preliminary release available within the next few months. Please contact me at mcfarljm at gmail if you would like to help with testing this feature.

-- July 2013

Known Issues