tags:

views:

2398

answers:

5

I have some numeric code that I need to convert to C or C++. I tried using f2c, but it won't work on the Fortran code. f2c complains because the code uses C style preprocessor directives (#include). The code's readme states that it is Fortran77, that works with the fort77 linker, that would expand those includes.

Does anyone know how to successfully convert this code?

My last resort is to write a simple preprocessor to expand those includes and then feed the code to f2c.

Note: I´m working in a Windows/Visual C++ environment here, so any gcc shenanigans would probably be more trouble than they are worth...

+7  A: 

This might be is going out a bit on a limb, but have you considered that since perhaps you're using C-style includes, you could actually run the C preprocessor on the file in order to include those files? Then, you could take that output and run it through f2c.

(I am not an expert on the matter. Please downvote this if appropriate.)

Paul Fisher
Great minds think alike.
Jonathan Leffler
Hun... I tough of that. But my compiler (Visual C++ 9.0) is not separate from the preprocessor... Or is there a command line to specify that?
David Reis
Its the /EP option of the command line compiler.
Robert Gamble
You may want to use a combination of the /C, /E, and /P options, see http://msdn.microsoft.com/en-us/library/becb7sys(VS.71).aspx for details.
Robert Gamble
+4  A: 

Have you no C pre-processor? On Unix, there might be a separate program, cpp, that would take the Fortran with #include directives and convert that into Fortran without #include directives. Alternatively, you could rename the source from xyz.f77 (xyz.f) to xyz.c, then run the C compiler in 'pre-processor only' mode and capture the output as the new input to the f2c program. You might have to worry about the options that eliminate #line directives in the output, etc, or you might be better off running the output through a simple filter (sed or perl spring to mind).

Jonathan Leffler
And fools seldom differ. ;)
Paul Fisher
Agin, how to use this preprocessor only mode in Visual C++ 9.0?
David Reis
Its the /EP option of the command line compiler.
Robert Gamble
+7  A: 

You might get away with manually converting the

#include "whatsit.f90"

to

INCLUDE 'whatsit.f90'

and then attempting the f2c conversion again.

boost
HA! Worth a shot!
David Reis
Even if it is Fortran 77 code (so the extension might be .f77 instead of .f90). It is certainly worth a shot.
Jonathan Leffler
Also, it would be better to do the conversion automatically, of course. Not a hard piece of scripting - probably a Perl one-liner.
Jonathan Leffler
+6  A: 

I worked in an engineering research group for many years. We never had good luck doing automated conversions from Fortran to C. The code was not particularly understandable and it was hard to maintain. Our best luck was using the Fortran code as a template for the algorithm and doing a re-implementation for anything that we expected to continue using. Alternatively, you could update the Fortran to use more modern Fortran constructs and get a lot of the same value that you would get from moving to C/C++. We also had some success calling Fortran routines from C, although the calling convention differences sometimes made things difficult.

tvanfosson
I have also found that f2c makes more sense as part of a build-process then for converting source to be subsequently edited by human beings.
dmckee