fable - Automatic Fortran to C++ conversion
Why converting Fortran to C++
Many legacy Fortran programs are still stable and running in the industry. It is often difficult to maintain and extend the legacy code (well, difficult compared with modern languages). I was developing an extension on a Fortran 77 program, but F77 does not support many important modern programming features, which really gave me a hetic. Converting old Fortran to C/C++, it is hoped that we can use C++ features to extend an old program, although it would not improve the readibilty of the code at all.
In this article commands are executed in Linux/Unix environment.
Things you need
- Install fable converter following the steps on the official website
- Download the cctbx library, in case if the included library lacks files)
Convert Fortran to C++
Don’t forget to source cctbx_env.sh
Use fable to convert a .f file to a C++ source file
The converter does not automatically “link” external functions. Placeholders of missing functions will be generated in absence of external function definition. The converted C++ code needs to be modified manually to replace the body of the external function with the real implementation. You may convert multiple Fortran files to one C++ file and sequentially separate C++ funtions to different files. Included files in Fortran will be automatically detected.
Problems
Dependency
It may return an error with dependency problems.
In most cases, we can solve this issue by including the library path:
If you did not find where the folder is, just download the entire project cctbx library and include the fable folder. If the library folder lacks tbxx, copy tbxx into the fable folder.
Equivalence
Equivalence and common vairables are commonly used for shared resources in Fortran 77. With fable version 1022, no more than 7 equivalent variables are allowed in conversion, that is, an equivalence cannot have more than 7 symbols.
The following code is convertible
The following code is currently inconvertible
One solution is to reduce the number of equivalence in Fortran before conversion.