16

As part of my Final Year Project, I need to convert some FORTRAN code into C or C++ (it doesn't matter which language as long as I can understand it, and I can understand C style languages).

I have discovered f2c, a program that allegedly converts FORTRAN to C, and tried to install it, following instructions, by saving a makefile.vc file on my drive and then doing

copy makefile.vc makefile
nmake

(here is the part of the README file about installing f2c that is included in the f2c download page)

To compile f2c on Linux or Unix systems, copy makefile.u to makefile, edit makefile if necessary (see the comments in it and below) and type "make" (or maybe "nmake", depending on your system).

To compile f2c.exe on MS Windows systems with Microsoft Visual C++,

copy makefile.vc makefile nmake

With other PC compilers, you may need to compile xsum.c with -DMSDOS (i.e., with MSDOS #defined).

If your compiler does not understand ANSI/ISO C syntax (i.e., if you have a K&R C compiler), compile with -DKR_headers .

On non-Unix systems where files have separate binary and text modes, you may need to "make xsumr.out" rather than "make xsum.out".

I am running x64 bit version of Windows Vista and tried "nmake", but I get

'nmake' is not recognised as an internal or external command, operable program or batch file.

I downloaded Nmake15.exe after some searching but it doesn't work on x64 bit machines and apparently there is not version of it that does. So I downloaded the Windows SDK, after being told that would work, but it didn't change anything.

Where have I gone wrong in all this, if I have, and is there any way of converting that FORTRAN code into C or C++?

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
OddCore
  • 1,534
  • 6
  • 19
  • 32
  • 13
    A word of warning: LanguageA-to-languageB-converters may produce code that runs correctly (although this alone can be quite hard), but about none of them produce *human-readable* code... –  Nov 30 '10 at 13:34
  • What do you propose then? I am open to suggestions, I just really have to make that FORTRAN code into C or C++ in some way... – OddCore Nov 30 '10 at 13:40
  • What version of Fortran? There have been several. If f2c hasn't changed, it converts Fortran 77. There are at least two newer versions. – David Thornley Nov 30 '10 at 20:21
  • 2
    @David Thornley: 4, actually (Fortran 90, 95, 2003, 2008). – janneb Dec 01 '10 at 14:51
  • 6
    I suppose actually _learning_ FORTRAN is out of the question . . . – geometrian Sep 26 '15 at 07:57

11 Answers11

12

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.

Community
  • 1
  • 1
LCFactorization
  • 1,652
  • 1
  • 25
  • 35
6

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!

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
4

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.

peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
  • I need to make a front-end for Zork in C++, but because I couldn't find Zork's source code, I went with Adventure, and found some FORTRAN source code. So I went with that. So yes, it is an assignment, my final year project, to be exact. – OddCore Dec 29 '10 at 12:04
  • 1
    This is exciting. I worked with Mike Arnautov - See http://en.wikipedia.org/wiki/Colossal_Cave_Adventure who developed later code in a more advanced language than FORTRAN. YSuggest you write to Mike, explain your problem and see what he suggests. the ADVENT code is brilliant - to get the whole game into fixed memory FORTRAN in the 1970's is genius. But you need to get into the spirit of the design. (Mike will probably know about Zork) – peter.murray.rust Dec 29 '10 at 12:35
  • 1
    Thank you sir, I will do just that. – OddCore Dec 29 '10 at 13:20
3

The method I've been using for conversion so far is semi-automated: lots of editor and text scripts.

First, collimate all the Fortran source into a single file (placing markers as appropriate to allow the files to be re-separated).

Next, convert the numeric labels to named labels (e.g. adding an L before them for gotos and F for formats). Make the appropriate changes in the places that reference the labels.

Next, mark off the label - continue combinations and the statement continuations (e.g. with a mark at the start of each line where they occur). Then re-join the label-continue and statement-continuation lines. In an editor like vi, that would mean doing a series of (say) 1000J's, and then a massive substitute :%s/#/^V^M/g if the start of line marker is a #. Since the continuation lines were specially marked as were the do-continue combos, then they get rejoined.

Ensure the if-labels and do-labels are separated (e.g. write a grep pattern to extract the relevant lines and then write a simple bracket-matching program to check them out). Once separated, you can then turn the do-continues into brackets.

Turn the if-then-else, and dowhile/enddo, subroutine/endsub, etc. into brackets. Make subroutine void, pony up fake names for the basic types (e.g. Boolean, Character, Integer, Real, Double, Complex, Complex).

Comments are converted over to C++ // comments; both the column 0 and ! comments.

Then you can re-prototype the functions. This is effectively a KR to ANSI-C conversion and requires a simple program to read the function headers and variable declarations to match them off. Ideally you may want to first put the variable declarations as one to a line. This also requires an edit script and a utility. For instance a "Double A, B, C, ..." would be converted to "#Double\nA\nB\n." and then a utility is set up to prefix all the non-# lines with the most recent preceding # line.

Parameter statements get rejoined to the type declarations and converted to "const" statements. Saves get converted to static declarations. Commons I haven't yet figured out a good way to work with.

Now the HARD parts come next: the array dimensioning, variable in/out analysis (to determine which variables need to be passed by value or by reference). You should get a C compiler that recognizes the reference type & or use C++. For array-redimensioning you may also need to go over to C++ which allows [] to be redefined.

The in-out analysis is something I haven't yet worked through, likewise the redimensioning. But at this point, the relevant source is mostly in C or C++ at this stage.

Be warned: Fortran 2010allows parallel processing, which goes beyond C and even C++ (except for their multithreaded abilities). If you programs have these in them, things will become a lot harder.

3

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.

David Thornley
  • 56,304
  • 9
  • 91
  • 158
2

Direct conversion to/from FORTRAN to C or to procedural version of C++ should not be complicated and is recommended mainly because you will learn the inner working of FORTRAN implementation.

Automatic tools like f2c will produce messy code with double underscore like this: __i (i being the loop counter variable).

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
beniekg
  • 68
  • 2
  • OP asked about the code translation at the end of his post. OP did not post any FORTRAN code example , nor any FORTRAN language constructs were mentioned in his post, so how would I provide more constructive answer to his question. – beniekg May 13 '16 at 10:15
  • Indeed. From the review queues it's sometimes not easy to see the full picture, sorry, it looked like it should have been a comment. – CherryDT May 13 '16 at 10:18
  • 1
    i sure hope the op finished his *final year* project by now! – agentp May 13 '16 at 11:11
  • OP failed to deliver on his final year project, so as his professor I flayed him alive and now wear his face. Do not be alarmed and do not adjust your set. – OddCore May 18 '16 at 10:03
2

If I had to install f2c on Windows I would use Cygwin. Now there is g77 I expect not many people use f2c any more.

  • `f2c` is still used where Fortran compilers aren't available, when C packages include Netlib sources, etc. It's much easier to install than `g77`. – Fred Foo Nov 30 '10 at 13:31
  • Well, I don't see the point of that. f2c just converts FORTRAN to C. How you compile it beyond that is a different matter. I personally have used both MS and Borland C compilers. But the choice is really immaterial to the conversion. – David Heffernan Nov 30 '10 at 19:30
2

I'm going to answer only the very last part of your question:

is there any way of converting that FORTRAN code into C or C++?

Yes there is: Read the fortran code, and write the equivalent C statements in a C file. They are both imperative languages. The syntax is different but not impossible to decode. This will likely give you much better results than an auto-converter, and will also give you an much deeper understanding of how the original program works.

AShelly
  • 34,686
  • 15
  • 91
  • 152
  • 18
    This answer boils down to: "How can I fix this bug?" - "Just rtfm and DIY, dude". This answer works to any SO question. In math we call it a "trivial solution". – sixtytrees Aug 06 '16 at 16:09
  • @sixtytrees folks were more generous with the upvotes towards the end of the last decade when this answer was posted.. – agentp Aug 06 '16 at 18:50
  • @agentp (1) I commented my downvote. (2) I upvoted AShelly's other post to keep the balance positive and because it was worth upvoting. How do you know my vote? – sixtytrees Aug 06 '16 at 20:36
  • 1
    @sixtytrees i don't know anything about votes. I don't know why you dredged up a 6 yr old question, but I just meant you are right this kind of "go read the manual" answer probably wouldn't be received so favorably today. – agentp Aug 06 '16 at 22:58
1

Have you tried the binary?

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • I downloaded that but it is a .gz file, and after downloading gnuzip, all I got was an .exe that flashes a command console at me and nothing more. – OddCore Nov 30 '10 at 13:38
  • You mean you double-clicked it? Both `gzip` and `f2c` are command-line applications. Get 7zip from http://7-zip.org if you want a GUI unzipper. – Fred Foo Nov 30 '10 at 13:48
  • Ok, I successfuly extracted the f2c.exe from that, though it's just a command console. I'll have to figure out how to operate it. (hoping I've done everything right so far; I've only downloaded the makefile from the original site. – OddCore Nov 30 '10 at 16:04
  • Very simple: run `f2c module.f` and you'll get a file `module.c` in the current directory. If something goes wrong, `f2c` will complain, else it will be silent. – Fred Foo Nov 30 '10 at 16:09
  • I got " Error on line 1: illegal continuation card (starts "f2c mo") " when I typed in " f2c module.f". Does that mean I am missing a file? I realise that it must be really frustrating, helping me with this, so I am very grateful for your help... – OddCore Nov 30 '10 at 16:29
  • I can't help you with this error without seeing your code. I probably can't help you even if you post your code, since I don't grok Fortran. Check out the `f2c` user manual at http://www.netlib.org/f2c/f2c.1 and note that to actually compile the resulting C code, you will need the `f2c.h` file and a Fortran runtime library. – Fred Foo Nov 30 '10 at 16:44
  • Well, I'll try my best to make sense of it, I couldn't post the FORTRAN code if I wanted, it's a whole program. Anyway, thank you for the help you provided. – OddCore Nov 30 '10 at 17:06
1

Do you have MS's Visual Studio installed? If so, nmake is available on the Visual Studio Command Line (not the normal command line). If not, perhaps try downloading and installing that?

TZHX
  • 5,291
  • 15
  • 47
  • 56
  • Yes, I do have MS Visual Studio 2010, I will try it with that and get back to you, here! – OddCore Nov 30 '10 at 13:34
  • I get "NMAKE: fatal error U1073: don't know how to make 'xsum.c'" as an error. Is that to do with the part of the README file about non-Unix systems where I have to "make xsumr.out"? – OddCore Nov 30 '10 at 13:36
1

The output of f2c could be converted into better readable C/C++ sources by using the f2cpp program which is available here.

It's written in Python and could be called together with f2c under Linux by e.g. a shell script

#!/bin/bash

f2c -a -C++ -p *.f
c2cpp.py *.c

where f2c converts the Fortran fixed format into C and c2cpp in human readable C/C++ sources.

License is GPL.

CKE
  • 1,533
  • 19
  • 18
  • 29