F2c Program: How to install, how to use, any hints, critique,

Do you have any information on the f2c program? It converts Fortran 77 source code to the C language.

I’m looking for detailed information about it.

Do you have any explicit examples that demonstrate what you like or dislike about it?

What else can you tell me about the f2c program?

I’m thinking about downloading it from here.

Thank you,

1 Like

F2c is intended to produce compilable C code, not readable and maintainable C code. Its main application is for a platform that does not have a compiler than can compile Fortran 77 code. Gfortran and other compilers can handle F77. So do you need to use f2c?

1 Like

Well, you use f2c to translate your FORTRAN 77 code to C:

$ f2c test.f

This creates the C file test.c, which just has to be compiled, and linked against the f2c runtime library, either dynamically against shared library libf2c.so:

$ cc -o test test.c -lf2c

Or, statically against libf2c.a:

$ cc -o test test.c /usr/local/lib/libf2c.a

(The path to libf2c.a may depend on the used operating system.)

If you can show an example of the F77 code that you are contemplating to convert with f2c, pertinent advice could be given.

The f2c program can handle very few extensions to standard Fortran 77. You will need to build the f2c library from sources before you can run programs translated from F77 to C, and you are likely to run into trouble doing so. Try this step first, before deciding that you should use f2c.

f2c is best considered as a Fortran 77 compiler using C as the intermediate language. In most cases it is better to use a modern Fortran compiler. f2c does work but it would be about the last option I would consider.

It was a viable solution in the mid 1990’s - before g77 was integrated into gcc with the EGCS project in 1997 - but the world has moved on.

I actually use f2c to send a program to a user that is not allowed to use fortran. It translates the 30,000 of standard conforming F77 code quickly and correctly. I’ve never had to examine the C source, but then I never examined the assembler source from gfortran. I’d like to use it for a few other users, but they don’t have the f2c library already installed, and are not capable of building it themselves. There are many environments where there is a short list of allowed languages and Fortran is usually not allowed by the local Department of Information Prevention.

I did spend a day or so attempting to append the full set of library routines to the C code, but was unable to do that. I first ran cpp to include all the header files, and then concatenated all the c files. It wasn’t sufficient. While this would involve a lot of unreachable code in the file I would send to the user, that would not be a problem for me or them. It seems a shame to require the library be installed - anyone with the library will likely have Fortran already.

f2c is really why I don’t begin using more modern Fortran constructs, but in truth the case statement is the only one I really miss.

Daniel Feenberg
http://taxsim.nber.org

1 Like

The Windows binary at Netlib runs on Windows 10, and for WSL2, sudo apt-get install f2c, worked. F2c translates loops with enddo, which was not in F77. The translation of

      integer function square(i)
      integer i
      square = i**2
      end

      program main
      integer i, isum, square
      isum = 0
      do i=1,3
         isum = isum + square(i)
      end do
      print*,"isum =",isum
      end

excluding some comments is

#include "f2c.h"

/* Table of constant values */

static integer c__9 = 9;
static integer c__1 = 1;
static integer c__3 = 3;

integer square_(integer *i__)
{
    /* System generated locals */
    integer ret_val, i__1;

/* Computing 2nd power */
    i__1 = *i__;
    ret_val = i__1 * i__1;
    return ret_val;
} /* square_ */

/* Main program */ int MAIN__(void)
{
    /* Builtin functions */
    integer s_wsle(cilist *), do_lio(integer *, integer *, char *, ftnlen), 
	    e_wsle(void);

    /* Local variables */
    static integer i__, isum;
    extern integer square_(integer *);

    /* Fortran I/O blocks */
    static cilist io___3 = { 0, 6, 0, 0, 0 };


    isum = 0;
    for (i__ = 1; i__ <= 3; ++i__) {
	isum += square_(&i__);
    }
    s_wsle(&io___3);
    do_lio(&c__9, &c__1, "isum =", (ftnlen)6);
    do_lio(&c__3, &c__1, (char *)&isum, (ftnlen)sizeof(integer));
    e_wsle();
    return 0;
} /* MAIN__ */

/* Main program alias */ int main_ () { MAIN__ (); return 0; }

George Levy of NAG wrote a 1995 paper Improving the output of the FORTRAN to C translator, f2c.

I used f2c in 1990’s to incorporate some Fortran code into software suites written in C. Now it could be done using F-C interoperability features. Recently I wanted to revive some old memories and found, quite surprisingly, that modern Linux distros still have f2c in their depositories.

BTW, the OP asked for detailed information on f2c and even quoted its URL at Netlib. I wonder if he/she bothered to read 27-pages, quite detailed user manual available there.

Yep. Despite my comments above, I would use f2c before I tried to fight corporate IT policies.

Always fight corporate IT policies. Tell them what they want is not possible and they have to allow Fortran. How are they going to know any better? Likely your time is more valuable than whoever is in charge of checking the box to allow it.

F2C is just an abomination that harms the Fortran ecosystem. It is best forgotten.

2 Likes