使用 f2c 从 Fortran 翻译的 C 代码中的错误

Error in a C code which is translated from Fortran using a f2c

我从来没有用 Fortran 编写过代码。所以我有一些与 Fortran 相关的工作,我在其中将该代码转换为 C。但是当我是 运行 C 代码时,它给了我以下错误:

/usr/lib/gcc/i686-linux-gnu/5/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/ccNX2LeT.o: In function `MAIN__':
sinai_static.c:(.text+0x58): undefined reference to `f_open'
sinai_static.c:(.text+0x2ca): undefined reference to `sqrt'
sinai_static.c:(.text+0x400): undefined reference to `sqrt'
sinai_static.c:(.text+0x487): undefined reference to `sqrt'
sinai_static.c:(.text+0x6f1): undefined reference to `atan2'
sinai_static.c:(.text+0x71b): undefined reference to `atan2'
sinai_static.c:(.text+0x777): undefined reference to `sqrt'
sinai_static.c:(.text+0x795): undefined reference to `cos'
sinai_static.c:(.text+0x7bb): undefined reference to `sin'
sinai_static.c:(.text+0x8ef): undefined reference to `s_wsle'
sinai_static.c:(.text+0x908): undefined reference to `do_lio'
sinai_static.c:(.text+0x921): undefined reference to `do_lio'
sinai_static.c:(.text+0x93a): undefined reference to `do_lio'
sinai_static.c:(.text+0x953): undefined reference to `do_lio'
sinai_static.c:(.text+0x96c): undefined reference to `do_lio'
sinai_static.c:(.text+0x974): undefined reference to `e_wsle'
sinai_static.c:(.text+0x9b5): undefined reference to `s_wsle'
sinai_static.c:(.text+0x9ce): undefined reference to `do_lio'
sinai_static.c:(.text+0x9fc): undefined reference to `do_lio'
sinai_static.c:(.text+0xa2e): undefined reference to `do_lio'
sinai_static.c:(.text+0xa60): undefined reference to `do_lio'
sinai_static.c:(.text+0xa92): undefined reference to `do_lio'
sinai_static.c:(.text+0xa9a): undefined reference to `e_wsle'
collect2: error: ld returned 1 exit status

旧的 Fortran 代码使用来自 ran0.f 的外部方法。我认为问题在于打开文件,因为翻译后的代码使用 f_open(&o_1) 会出错。但我不知道如何解决它。 我附上C代码以供参考。

    #include "f2c.h"

/* Table of constant values */

static integer c__3 = 3;
static integer c__1 = 1;
static integer c__5 = 5;

/* Main program */ int MAIN__(void)
{
    /* System generated locals */
    integer i__1;
doublereal d__1, d__2, d__3, d__4;
olist o__1;

/* Builtin functions */
integer f_open(olist *);
double sqrt(doublereal), atan2(doublereal, doublereal), cos(doublereal), 
    sin(doublereal);
integer s_wsle(cilist *), do_lio(integer *, integer *, char *, ftnlen), 
    e_wsle(void);

/* Local variables */
static doublereal a, c__, l, m, r__, e0, u0, u1, w0, x0, x1, z0, z1, x2, 
    z2, w1, u2, w2, t2;
static integer ii;
static doublereal en[500000], pi;
static integer it;
static doublereal ts, total_time__, vel;
extern doublereal ran0_(integer *);
static doublereal enke[500000];
static integer bhit;
static doublereal enpe[500000];
static integer idum, iter, itmin;
static doublereal ttemp, xtemp, ztemp, tstep;
static integer total_itcount__;
static doublereal maxvel, tanglec, tanglei, tangler;
static integer t_write__;

/* Fortran I/O blocks */
static cilist io___43 = { 0, 6, 0, 0, 0 };
static cilist io___44 = { 0, 18, 0, 0, 0 };


o__1.oerr = 0;
o__1.ounit = 18;
o__1.ofnmlen = 24;
o__1.ofnm = "data/sinai_static_r7.txt";
o__1.orl = 0;
o__1.osta = "unknown";
o__1.oacc = 0;
o__1.ofm = 0;
o__1.oblnk = 0;
f_open(&o__1);
pi = 3.14159265358979f;
idum = 120;
/* cc      POLYGON PARAMETERS */
    r__ = .7f;
    l = 4.f;
/* cc      INITIAL CONDITIONS */
    x0 = .3f;
    z0 = .33f;
    u0 = 100.f / sqrt(2.f);
    w0 = u0 * .1f;
/* Computing 2nd power */
    d__1 = u0;
/* Computing 2nd power */
    d__2 = w0;
    e0 = d__1 * d__1 + d__2 * d__2;
/*         write(*,*) "r=",r */

/*         write(*,*) "L=",L */
    tstep = l * .5f / u0;
    itmin = 500000;
    total_itcount__ = 0;
    for (iter = 1; iter <= 500000; ++iter) {
        en[499999] = 0.f;
        enke[499999] = 0.f;
        enpe[499999] = 0.f;
    }
    for (ii = 1; ii <= 1000; ++ii) {
/*            z0=0.3*(1.0+PI*(iI-1)/NI) */
    x0 = ran0_(&idum);
    if (x0 < .5f) {
        x0 = -(r__ + x0 * 2.f * (l - r__));
    } else {
        x0 = r__ + x0 * 2.f * (l - r__);
    }
    z0 = ran0_(&idum);
    if (z0 < .5f) {
        z0 = -(r__ + z0 * 2.f * (l - r__ - a));
    } else {
        z0 = r__ + z0 * 2.f * (l - r__);
    }
/*            write(*,*) iI,x0,z0 */
    x2 = x0;
    z2 = z0;
    u2 = u0;
    w2 = w0;
/* Computing 2nd power */
    d__1 = u2;
/* Computing 2nd power */
    d__2 = w2;
    vel = sqrt(d__1 * d__1 + d__2 * d__2);
    bhit = 0;
    total_time__ = 0.f;
    maxvel = 0.f;
    it = 0;
    t_write__ = 0;
    for (iter = 1; iter <= 500000; ++iter) {
/*            if((mod(iter,100000)).eq.0.0) write(*,*) iter */
        x1 = x2;
        z1 = z2;
        u1 = u2;
        w1 = w2;
        ts = -x1 / u1;
        m = w1 / u1;
        c__ = z1 + w1 * ts;
        if (u1 > 0.f) {
/* Computing 2nd power */
        d__1 = r__;
/* Computing 2nd power */
        d__2 = m;
/* Computing 2nd power */
        d__3 = c__;
/* Computing 2nd power */
        d__4 = m;
        x2 = (-m * c__ - sqrt(d__1 * d__1 * (d__2 * d__2 + 1) - d__3 *
             d__3)) / (d__4 * d__4 + 1);
        } else {
/* Computing 2nd power */
        d__1 = r__;
/* Computing 2nd power */
        d__2 = m;
/* Computing 2nd power */
        d__3 = c__;
/* Computing 2nd power */
        d__4 = m;
        x2 = (-m * c__ + sqrt(d__1 * d__1 * (d__2 * d__2 + 1) - d__3 *
             d__3)) / (d__4 * d__4 + 1);
        }
        t2 = (x2 - x1) / u1;
        if (t2 > 0.f) {
        bhit = 3;
        } else {
        if (u1 > 0.f) {
            ttemp = (l - x1) / u1;
        } else {
            ttemp = (-l - x1) / u1;
        }
        xtemp = x1 + ttemp * u1;
        ztemp = z1 + ttemp * w1;
        t2 = 0.f;
        if (ztemp < -l) {
            t2 = (-l - z1) / w1;
            bhit = 0;
        } else if (ztemp > l) {
            t2 = (l - z1) / w1;
            bhit = 2;
        } else {
            t2 = ttemp;
            bhit = 1;
        }
        }
        x2 = x1 + t2 * u1;
        z2 = z1 + t2 * w1;
        total_time__ += t2;
        if (bhit == 0) {
        u2 = u1;
        w2 = -w1;
        } else if (bhit == 1) {
        u2 = -u1;
        w2 = w1;
        } else if (bhit == 2) {
        u2 = u1;
        w2 = -w1;
        } else if (bhit == 3) {
        tanglec = atan2(z2, x2);
        tanglei = atan2(w1, u1);
        tangler = pi + tanglec * 2.f - tanglei;
    /* Computing 2nd power */
            d__1 = u1;
    /* Computing 2nd power */
            d__2 = w1;
            vel = sqrt(d__1 * d__1 + d__2 * d__2);
            u2 = cos(tangler) * vel;
            w2 = sin(tangler) * vel;
            }
    /*               if(total_time.gt.(iT*tstep)) then */
            ++it;
    /* Computing 2nd power */
            d__1 = u2;
    /* Computing 2nd power */
            d__2 = w2;
            en[it - 1] = en[it - 1] + d__1 * d__1 + d__2 * d__2;
    /* Computing 2nd power */
            d__1 = w2;
            enke[it - 1] += d__1 * d__1;
    /* Computing 2nd power */
            d__1 = u2;
            enpe[it - 1] += d__1 * d__1;
    /*               endif */
    /*               write(18,*) iter,x2,z2,u2,w2 */
        }
    /*            write(18,*) '' */
        if (itmin > it) {
            itmin = it;
        }
        if (ii % 100 == 0) {

        s_wsle(&io___43);
        do_lio(&c__3, &c__1, (char *)&ii, (ftnlen)sizeof(integer));
        do_lio(&c__5, &c__1, (char *)&total_time__, (ftnlen)sizeof(
            doublereal));
        do_lio(&c__3, &c__1, (char *)&iter, (ftnlen)sizeof(integer));
        do_lio(&c__5, &c__1, (char *)&u2, (ftnlen)sizeof(doublereal));
        do_lio(&c__5, &c__1, (char *)&w2, (ftnlen)sizeof(doublereal));
        e_wsle();
    }
    }
    i__1 = itmin;
    for (iter = 1; iter <= i__1; ++iter) {
    s_wsle(&io___44);
    do_lio(&c__3, &c__1, (char *)&iter, (ftnlen)sizeof(integer));
    d__1 = iter * tstep;
    do_lio(&c__5, &c__1, (char *)&d__1, (ftnlen)sizeof(doublereal));
    d__2 = en[iter - 1] / 1000;
    do_lio(&c__5, &c__1, (char *)&d__2, (ftnlen)sizeof(doublereal));
    d__3 = enke[iter - 1] / 1000;
    do_lio(&c__5, &c__1, (char *)&d__3, (ftnlen)sizeof(doublereal));
    d__4 = enpe[iter - 1] / 1000;
    do_lio(&c__5, &c__1, (char *)&d__4, (ftnlen)sizeof(doublereal));
    e_wsle();
    }
    return 0;
} /* MAIN__ */

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

我的理解是f2c,在翻译文件的开头,一般会生成如下header(文件名和f2c版本号可能不同):

/* helloworld.f -- translated by f2c (version 20160102).
   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
*/

请遵循这些说明(与@zwol 的建议一致)以正确编译和 link 您的源代码。

  • 你需要link针对f2c库(-lf2clibf2c.a),因为它提供了f_opendo_lioe_wsles_wsle 由 f2c 生成的代码使用。
  • 您需要针对 C 数学库 (-lm) link,因为它提供了您的代码使用的数学函数 (sqrtsincos, 和 atan2).

除此之外,您还需要编译另一个源文件 ran0.f,并针对它 link。

如果您使用的是 GCC,从您报告的错误形式来看,要遵循的步骤是:

  1. 使用 Fortran 编译器编译 Fortran 文件ran0.f

    $ gfortran ran0.f -c -o ran0.o
    
  2. 编译您提供的源代码文件;假设它的名字是 foo.c,

    $ gcc foo.c -c -o foo.o
    
  3. 通过 linking 针对 f2c 库创建二进制文件 (foo);假设它安装在您操作系统的标准位置,

    $ gcc foo.o ran0.o -lf2c -lm -o foo
    

    否则,您必须找到文件 libf2c.a 在系统中的位置(例如,/home/myuser/code/f2c/lib/libf2c.a)并在 linking 命令中使用该路径,而不是 -lf2c:

    $ gcc foo.o ran0.o /home/myuser/code/f2c/lib/libf2c.a -lm -o foo
    

如果您还想将文件 ran0.f 转换为 C,而不是上面的步骤 1。您应该首先使用 f2c 将 Fortran 源文件 ran0.f 转换为 C 源文件

$ f2c ran0.f

(这创建文件ran0.c)然后编译C源文件创建目标文件ran0.o

$ gcc ran0.c -c -o ran0.o

然后继续执行上述步骤 2. 和 3.。