Home Objexx Engineering
 

Fortran to C++ Conversion

Objexx Engineering offers a Fortran to C++ conversion service based on principles and tools originally developed to migrate our own applications. The goal of our conversion is to preserve the syntax and semantics of the Fortran within fully ANSI/ISO compliant C++ to preserve the value of the code and existing documentation and to allow a wide range of developers to maintain the code.

Some of the technical aspects of our conversion include:

  • Reference (not pointer!) argument passing
  • Variable names are preserved
  • Fortran-like array template classes provide column-major arrays that preserve the element indexing of the Fortran and Fortran's array passing "tricks"
  • Fortran-like string and substring classes are provided to encapsulate the distinct properties of Fortran strings and allow these strings to work smoothly with C++ strings
  • DATA statements become constructor initializers
  • COMMON blocks are converted to global namespaces where possible
  • EQUIVALENCEs and COMMONs that can introduce aliasing are detected and converted as appropriate for the intent of the usage

The Objexx Fortran Compatibility Library (ObjexxFCL) that provides the array, string, and intrinsic function support is included in C++ source form with licensing that allows modification by the client for use with future versions of their project.


Conversion Sample

This conversion of a simple Fortran 77 routine to C++ gives some sense of our approach. The philosophy of this method is to maintain the syntax and semantics of the Fortran as much as possible, thereby allowing both C++ and Fortran developers to maintain the code.

Fortran Subroutine

C============================================
C Multiply Matrix by Vector: b = A x
C============================================
SUBROUTINE MULTAX( M, N, A, X, B )

! Argument Declarations INTEGER M, N REAL A(M,N) REAL X(N) REAL B(M)
! Variable Declarations INTEGER I, J REAL BI

! Compute b = A x DO I = 1, M BI = 0.0 DO J = 1, N BI = BI + A(I,J) * X(J) END DO B(I) = BI END DO

END



C++ Conversion

#include "multAx.h"
#include "FArray.h"

//=========================================== // Multiply Matrix by Vector: b = A x //=========================================== void MULTAX( int M, int N, FArray2D_f & A, FArray1D_f & X, FArray1D_f & B ) { // Compute b = A x for ( int I = 1; I <= M; ++I ) { float BI = 0.0; for ( int J = 1; J <= N; ++J ) { BI += A(I,J) * X(J); } B(I) = BI; } }