The SWIG-Fortran project is an extension of the Simplified Wrapper and Interface Generator (SWIG) tool, specifically designed to generate Fortran 2003 bindings for C and C++ libraries. This allows Fortran applications to seamlessly interface with existing C/C++ codebases, which is particularly valuable for scientific computing, high-performance computing (HPC), and engineering applications where Fortran remains prevalent. Below are summarized key details about the project based on the provided references and additional context from the web results.
SWIG-Fortran automates the creation of native Fortran 2003 interfaces to C and C++ libraries, enabling Fortran developers to leverage robust and efficient C/C++ libraries without manual wrapper coding. It addresses the challenge of integrating modern Fortran with C/C++ codebases, which often contain optimized algorithms or standard library components. The tool parses C/C++ header files and generates Fortran wrapper code (glue code) that provides a natural Fortran interface to the underlying C/C++ functionality. This includes support for:
swig -fortran -help
to confirm Fortran support.Quick reference for generating Fortran 2003 bindings to C/C++ libraries using SWIG-Fortran, based on the SWIG+Fortran User Manual.
.i
) for your C/C++ library.example.i
): example_wrap.c
(C wrapper), example.f90
(Fortran module).apply
macro to change the constant type fortranconst
directive can be used to explicitly enable the native compile-time constant. Values are rendered as named constants Standard layout C-struct can be shared with Fortran code natively using bind(c)
derived types. By doing so, the struct
becomes
This can be obtained by using the macro
Every member of the struct must be bind(c)
compatible. This is enforced with a separate typemap(bindc)
that translates the member data to Fortran type members
Finally, the fortranbindc
feature allows natively interoperable types to be directly accessed in the Fortran code.