Converting FORTRAN to C / C++

You aren't real clear on this, but it sounds like you want to study the code, and in order to do that you want to convert it to C or C++. If you want to use the code rather than study it, then I'm off-base here, and you should look for Fortran compilers.

Give up. You can't find any way of translating idiomatic Fortran to idiomatic C or C++. There's subtle semantic differences between the languages, and in order to get it right the translation program needs to be very precise, and account for lots of things, and make obscure function calls, and that can be very confusing. You will not get readable C or C++.

Instead, you should learn the appropriate version of Fortran. If you understand C and C++, it shouldn't be at all hard to learn. It's just another language, and it won't have much in the way of new concepts.


I found there is a small toolkit named fable fable - Automatic Fortran to C++ conversion which is dedicated to such conversion.

THere is also a review of such a tool. Abstract from the review authors:

Background

In scientific computing, Fortran was the dominant implementation language throughout most of the second part of the 20th century. The many tools accumulated during this time have been difficult to integrate with modern software, which is now dominated by object-oriented languages.

Results

Driven by the requirements of a large-scale scientific software project, we have developed a Fortran to C++ source-to-source conversion tool named FABLE. This enables the continued development of new methods even while switching languages. We report the application of FABLE in three major projects and present detailed comparisons of Fortran and C++ runtime performances.

Conclusions

Our experience suggests that most Fortran 77 codes can be converted with an effort that is minor (measured in days) compared to the original development time (often measured in years). With FABLE it is possible to reuse and evolve legacy work in modern object-oriented environments, in a portable and maintainable way. FABLE is available under a nonrestrictive open source license. In FABLE the analysis of the Fortran sources is separated from the generation of the C++ sources. Therefore parts of FABLE could be reused for other target languages.


f2c really is the way to go, provided you have F77 code. If you have F90 or later then f2c won't help. I've used f2c many many times with great success, so long as you remember the -a switch.

As an aside I would rate f2c as one of the all time great codes!


You need to be clear why you are doing this. Is it because the functionality you need is ONLY available in some legacy FORTRAN? I had this problem many years ago when I needed a general matrix inversion algorithm, that was only available in FORTRAN. It wasn't easy to understand - no comments and variables named like G(J). I converted it to C using f2c and it ran perfectly. But it was even harder to understand. Two points were that FORTRAN counts from 1 and C from 0, so there were lots of i+1 and j-1. Also arguments had to be implemented by reference.

Later I had to run this in Java. Still no other algorithm, so I converted the C to Java. This was really painful. And I still didn't understand what has happening.

And after a year or two it stopped working!

But, luckily, now there are several Java implementations.

So if you can explain your real requirements maybe we can help. I hope that this isn't an assignment, because if so it's (IMO) a poor one. If there is some magic legacy code, suggest you try as hard as possible to find a modern equivalent.