使用f2c从Fortran转换的C代码中的错误

时间:2018-06-15 15:03:37

标签: c fortran fortran90 fortran77

我从未在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; }

1 个答案:

答案 0 :(得分:1)

我的理解是f2c,在翻译文件的开头,通常会生成以下标题(文件名和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建议一致)正确编译和链接您的源代码。

  • 您需要链接f2c库(-lf2clibf2c.a),因为它提供的功能包括f_opendo_lioe_wsle或{{ 1}}由f2c生成的代码使用。
  • 您需要链接C数学库(s_wsle),因为它提供了代码使用的数学函数(-lmsqrtsin和{ {1}})。

除此之外,您还需要编译其他源文件cos,并对其进行链接。

如果您使用的是GCC,就像您报告的错误形式一样,请遵循以下步骤:

  1. 使用Fortran编译器编译Fortran文件atan2

    ran0.f
  2. 编译您提供源代码的文件;假设其名称为ran0.f

    $ gfortran ran0.f -c -o ran0.o
    
  3. 通过链接f2c库创建二进制文件(foo.c);假设它安装在操作系统的标准位置,

    $ gcc foo.c -c -o foo.o
    

    否则,您必须找到文件foo在系统中的位置(例如$ gcc foo.o ran0.o -lf2c -lm -o foo )并在链接命令中使用该路径,而不是libf2c.a

    /home/myuser/code/f2c/lib/libf2c.a
  4. 如果您还希望将文件-lf2c转换为C,而不是上面的步骤1.您应首先使用$ gcc foo.o ran0.o /home/myuser/code/f2c/lib/libf2c.a -lm -o foo 将Fortran源文件ran0.f转换为C源文件< / p>

    f2c

    (这会创建文件ran0.f),然后编译C源文件以创建目标文件$ f2c ran0.f

    ran0.c

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