Cc f2c output: undefined reference to 'MAIN__'

What am I doing wrong? To learn how to correctly do it, I’ve run f2c on a very simple Fortran (main.f) program to produce the C equivalent file (main.c), but it won’t compile with the cc command.

File main.f:

        PROGRAM TEST
        WRITE(*,*)
        WRITE(*,'(A$)')' <rtn> to continue.'
        END

f2c executable being run on the main.f file:

f2c main.f 

main.c is the output from running f2c:

/* main.f -- translated by f2c (version 20200916).
   You must link the resulting object file with libf2c:
	on Microsoft Windows system, link with libf2c.lib;
	on Linux or Unix systems, link with .../path/to/libf2c.a -lm
	or, if you install libf2c.a in a standard place, with -lf2c -lm
	-- in that order, at the end of the command line, as in
		cc *.o -lf2c -lm
	Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,

		http://www.netlib.org/f2c/libf2c.zip
*/

#include "f2c.h"

/* Table of constant values */

static integer c__1 = 1;

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

    /* Fortran I/O blocks */
    static cilist io___1 = { 0, 6, 0, 0, 0 };
    static cilist io___2 = { 0, 6, 0, "(A$)", 0 };


    s_wsle(&io___1);
    e_wsle();
    s_wsfe(&io___2);
    do_fio(&c__1, " <rtn> to continue.", (ftnlen)19);
    e_wsfe();
    return 0;
} /* MAIN__ */

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

Compilation command:

cc -o main libf2c.a -lm -DLINUX -DSYSV -DNOHLA -g -Wall -Werror -fmax-errors=1
echo 

Error:

libf2c.a(main.o): In function `main':
(.text+0x155): undefined reference to `MAIN__'
collect2: error: ld returned 1 exit status

I think main.c should appear before libf2c.a in your compilation command.

1 Like

You could copy the file f2c.h to the directory where you are compiling.

1 Like