Fortran编译问题

问题描述 投票:0回答:1

我是fortran的新手,我必须运行一些文件。我安装了MinGW,并按照Windows 10上youtube视频https://www.youtube.com/watch?v=xuQL_BZydS0的说明进行操作我添加了环境路径。我打开命令提示符,转到我有文件的文件夹,然后键入命令gfortran -O3 reader.f iotools.c -o reader.x我遇到了上面写的一些错误,并尝试通过在其提到的函数和#include <stdlib.h>的前面添加int来修复它们,但是我不确定自己是否正确。我附上的文件iotools.c如下所示

C:\Users\user\Desktop\Ex>gfortran -O3 reader.f iotools.c -o reader.x
iotools.c: In function 'iwritecfw_':
iotools.c:85:11: warning: implicit declaration of function 'exit' [-Wimplicit-function-declaration]
           exit(1);}
           ^~~~
iotools.c:85:11: warning: incompatible implicit declaration of built-in function 'exit'
iotools.c:85:11: note: include '<stdlib.h>' or provide a declaration of 'exit'
iotools.c: In function 'idpwritecfw_':
iotools.c:98:11: warning: incompatible implicit declaration of built-in function 'exit'
           exit(1);}
           ^~~~
iotools.c:98:11: note: include '<stdlib.h>' or provide a declaration of 'exit'
iotools.c: In function 'ireadcfr_':
iotools.c:134:11: warning: incompatible implicit declaration of built-in function 'exit'
           exit(1);}
           ^~~~
iotools.c:134:11: note: include '<stdlib.h>' or provide a declaration of 'exit'
iotools.c: In function 'idpreadcfr_':
iotools.c:147:11: warning: incompatible implicit declaration of built-in function 'exit'
           exit(1);}
           ^~~~
iotools.c:147:11: note: include '<stdlib.h>' or provide a declaration of 'exit'
iotools.c: At top level:
iotools.c:174:1: warning: return type defaults to 'int' [-Wimplicit-int]
 idumpc_(vector,nbytes)
 ^~~~~~~
iotools.c:184:1: warning: return type defaults to 'int' [-Wimplicit-int]
 idpdumpc_(vector,nbytes)
 ^~~~~~~~~
iotools.c:195:1: warning: return type defaults to 'int' [-Wimplicit-int]
 ireadc_(vector,nbytes)
 ^~~~~~~
iotools.c:205:1: warning: return type defaults to 'int' [-Wimplicit-int]
 idpreadc_(vector,nbytes)
 ^~~~~~~~~

iotools.c的初始文件

/* iotools.c utilities, to read and write binary C from FORTRAN */



#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#ifndef O_BINARY
#define O_BINARY 0
#endif 

FILE *fw ;  /* pointers to the files used for writing   */
FILE *fr ;  /*                  and reading             */


/******* OPENING AND CLOSING ROUTINES ******************************************/

void openfilew_(namef)
/* opens for binary writting a file with fortran name namef. First translate the 
fortran string namef to the C string namec . 
>From fortran it is called as call openfilew(namef)  */
 char *namef;
{
 char namec[80];
 int i;
 strncpy(namec,namef,80);
/* character strings in fortran are filled with empty spaces. In C, they 
should finish with \0 . Thus, translate */
 i=0;
 for ( ;namec[i] != ' ' ; ++i){}
 namec[i]='\0';
 fw=fopen(namec,"wb");       /* this is the actual file opening */
 if (fw == NULL) {fprintf(stderr,"Error opening file %s \n",namec);  } 
 return ;
}


void openfiler_(namef)
/* opens for binary reading a file with fortran name namef. First translate the 
fortran string namef to the C string namec . 
>From fortran it is called as call openfiler(namef)   */
 char *namef;
{
 char namec[80];
 int i;
 strncpy(namec,namef,80);
/* character strings in fortran are filled with empty spaces. In C, they 
should finish with \0  */ 
 i=0;
 for ( ;namec[i] != ' ' ; ++i){}
 namec[i]='\0';
 fr=fopen(namec,"rb");     /* this is the actual file opening */
 if (fr == NULL) {fprintf(stderr,"Error opening file %s \n",namec);  } 
 return ;}


void closefilew_(void)
/* close the writing file opened by openfilew. From fortran 
it is called as call closefilew()    */
{fclose(fw);
return;
}

void closefiler_(void)
/* close the reading file opened by openfiler. From fortran 
it is called as call closefiler()    */
{fclose(fr);
return;
}

/******* WRITING ROUTINES ******************************************/

int iwritecfw_(vector,nelements)
/* writes in binary C to the file fw opened for writing a float 
    vector of nelements. It returns the number of elements (nelements) written. 
>From fortran it is called as icontrol=iwritecfw(vector,nelements)  , with 
vector a single precision float  */
 float *vector;
 int *nelements;
{
if (fw == NULL){
          fprintf(stderr,"C-binary file is not open to read\n");
          exit(1);}
 return fwrite(vector,4,*nelements,fw);
}


int idpwritecfw_(vector,nelements)
/* idem, but vector is a double precision float
>From fortran it is called as icontrol=idpwritecfw(vector,nelements)  */
 double *vector;
 int *nelements;
{
if (fw == NULL){
          fprintf(stderr,"C-binary file is not open to read\n");
          exit(1);}
 return fwrite(vector,8,*nelements,fw);
}


int iwritecstdout_(vector,nelements)
/* writes in binary C to stdout a float 
    vector of nelements. It returns the number of elements (nelements) written. 
>From fortran it is called as icontrol=iwritecstdout(vector,nelements). Under cygwin, first
fix the stdout stream to binary by using fixstd() */
 float *vector;
 int *nelements;
{ return fwrite(vector,4,*nelements,stdout); }


int idpwritecstdout_(vector,nelements)
/* idem, but vector is a double precision float
>From fortran it is called as icontrol=idpwritecstdout(vector,nelements). Under cygwin, first
fix the stdout stream to binary by using fixstd() */
 double *vector;
 int *nelements;
{ return fwrite(vector,8,*nelements,stdout); }


/******* READING ROUTINES *******************************************/

int ireadcfr_(vector,nelements)
/* reads in binary C from the file fr opened for reading a float 
    vector of nelements. It returns the number of elements (nelements) read. 
>From fortran it is called as icontrol=ireadcfr(vector,nelements) , with 
vector a single precision float.   */
 float *vector;
 int *nelements;
{
 if (fr == NULL){
          fprintf(stderr,"C-binary file is not open to read\n");
          exit(1);}
 return fread(vector,4,*nelements,fr);
}


int idpreadcfr_(vector,nelements)
/* idem, but vector is a double precision float. 
>From fortran it is called as icontrol=idpreadcfr(vector,nelements).   */
 double *vector;
 int *nelements;
{
 if (fr == NULL){
          fprintf(stderr,"C-binary file is not open to read\n");
          exit(1);}
 return fread(vector,8,*nelements,fr);
}


int ireadcstdin_(vector,nelements)
/* reads in binary C from stdin a float 
    vector of nelements. It returns the number of elements (nelements) read. 
>From fortran it is called as icontrol=ireadcstdin(vector,nelements). Under cygwin, first
fix the stdin stream to binary by using fixstd() */
 float *vector;
 int *nelements;
{ return fread(vector,4,*nelements,stdin); }


int idpreadcstdin_(vector,nelements)
/* idem, but vector is a double precision float.  
>From fortran it is called as icontrol=idpreadcstdin(vector,nelements). Under cygwin, first
fix the stdin stream to binary by using fixstd() */
 double *vector;
 int *nelements;
{ return fread(vector,8,*nelements,stdin); }


/******* The following are non-ANSI C functions   *****************/


idumpc_(vector,nbytes)
/* the same as iwritecstdout , but using write instead of fwrite. nbytes is the 
number of bytes, i.e.  4*nelements. It returns the number of bytes written.
>From fortran it is called as icontrol=idumpc(vector,nbytes). 
Under cygwin, first fix the stdout stream to binary by using fixstd()  */
  float *vector;
  int *nbytes; 
  {return( write(1,vector,*nbytes) );}   
/* 0 is stdin, 1 is stdout, 2 is stderr */

idpdumpc_(vector,nbytes)
/* idem, but vector is double precision. nbytes is the 
number of bytes, i.e.  8*nelements. It returns the number of bytes written.
>From fortran it is called as icontrol=idpdumpc(vector,nbytes). 
Under cygwin, first fix the stdout stream to binary by using fixstd()  */
  double *vector;
  int *nbytes; 
  {return( write(1,vector,*nbytes) );}   
/* 0 is stdin, 1 is stdout, 2 is stderr */


ireadc_(vector,nbytes)
/* the same as ireadcstdin , but using read instead of fread. nbytes is the 
number of bytes, i.e.  4*nelements. It returns the number of bytes read. 
>From fortran it is called as icontrol=ireadc(vector,nbytes).
Under cygwin, first fix the stdin stream to binary by using fixstd()   */
  float *vector;
  int *nbytes;
  {return( read(0,vector,*nbytes) );}
/* 0 is stdin, 1 is stdout, 2 is stderr */

idpreadc_(vector,nbytes)
/* idem, but vector is double precision. nbytes is the 
number of bytes, i.e.  8*nelements. It returns the number of bytes read. 
>From fortran it is called as icontrol=idpreadc(vector,nbytes).
Under cygwin, first fix the stdin stream to binary by using fixstd()   */
  double *vector;
  int *nbytes;
  {return( read(0,vector,*nbytes) );}
/* 0 is stdin, 1 is stdout, 2 is stderr */


/* void fixstd_(void)
use this only under cygwin (unix-like environment for windows) and only before 
using iwritecstdout and/or ireadcstdin (or ireadc or idumpc) redirected to local files. 
It changes the default mode from text to binary. 
It will do nothing under other circunstances (but O_BINARY will be undefined in most 
non-cygwin environments, so comment out this function when compiling there) .
{ setmode(1,O_BINARY);
 setmode(0,O_BINARY);   }  
 0 is stdin, 1 is stdout, 2 is stderr */
c mingw gfortran
1个回答
0
投票

[这段代码显然是在你们许多人出生之前编写的:它仍然具有ANSI之前的函数定义,并且看到注释行以>From开头告诉我这段代码在电子邮件系统中运行了一段时间。

首先,修复原型。在C的原始版本中,函数named的参数放在函数括号中,但在单独的行中设置其type

ireadc_(fort_str,fort_len)  // we don't
int *fort_len;              // .. do it this way
float *fort_str;            // .... any more
{
    return( read(0,fort_str,*fort_len) );
}

从C ++借用的函数原型将名称和类型移到括号中,并且需要返回类型(即使void也是如此:]]

int ireadc_(float *fort_str, int *fort_len)
{
    return( read(0, fort_str, *fort_len) );
}

整个iotools.c需要重新研究,因此我通过了一下:这包括添加一些#include文件,以支持所有调用的库函数;对于使用gcc的64位Linux系统,它可以为我完全编译。

我确实协调了一些评论,但并没有真正看待逻辑,其中一些可以改善。

/* iotools.c utilities, to read and write binary C from FORTRAN */

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
// #ifndef O_BINARY
// #define O_BINARY 0
// #endif

FILE *fw ;  /* pointers to the files used for writing   */
FILE *fr ;  /*                  and reading             */


/******* OPENING AND CLOSING ROUTINES ******************************************/

// opens for binary writting a file with fortran name namef. First translate the
// fortran string namef to the C string namec .
// From fortran it is called as call openfilew(namef)

void openfilew_(char *namef)
{
    char namec[80];

    strncpy(namec, namef, sizeof namec);

    // character strings in fortran are filled with empty spaces. In C, they
    // should finish with \0 . Thus, translate
    int i = 0;

    for ( ;namec[i] != ' ' ; ++i){}
    namec[i]='\0';
    fw = fopen(namec,"wb");       /* this is the actual file opening */
    if (fw == NULL) {fprintf(stderr,"Error opening file %s \n",namec);  }
}

// opens for binary reading a file with fortran name namef. First translate the
// fortran string namef to the C string namec .
// From fortran it is called as call openfiler(namef)

void openfiler_(char *namef)
{
    char namec[80];

    strncpy(namec, namef, sizeof namec);
    // character strings in fortran are filled with empty spaces. In C, they
    // should finish with \0
    int i = 0;
    for ( ;namec[i] != ' ' ; ++i){}
    namec[i]='\0';
    fr = fopen(namec, "rb");     /* this is the actual file opening */
    if (fr == NULL) {fprintf(stderr,"Error opening file %s \n",namec);  }
}


// close the writing file opened by openfilew. From fortran
// it is called as call closefilew()

void closefilew_(void)
{
    fclose(fw);
}

// close the reading file opened by openfiler. From fortran
// it is called as call closefiler()

void closefiler_(void)
{
    fclose(fr);
}

/******* WRITING ROUTINES ******************************************/

// writes in binary C to the file fw opened for writing a float
// vector of nelements. It returns the number of elements (nelements) written.
// From fortran it is called as icontrol=iwritecfw(vector,nelements)  , with
// vector a single precision float

int iwritecfw_(float *vector, int *nelements)
{
    if (fw == NULL) {
          fprintf(stderr,"C-binary file is not open to read\n");
          exit(1);
    }

    return fwrite(vector, 4, *nelements, fw);
}

// idem, but vector is a double precision float
// From fortran it is called as icontrol=idpwritecfw(vector,nelements)

int idpwritecfw_(double *vector, int *nelements)
{
    if (fw == NULL){
        fprintf(stderr,"C-binary file is not open to read\n");
        exit(EXIT_FAILURE);
    }
    return fwrite(vector, 8, *nelements, fw);
}


// writes in binary C to stdout a float
// vector of nelements. It returns the number of elements (nelements) written.
// From Fortran it is called as icontrol=iwritecstdout(vector,nelements).
// Under cygwin, first fix the stdout stream to binary by using fixstd()

int iwritecstdout_(float *vector, int *nelements)
{
    return fwrite(vector, 4, *nelements, stdout);
}

// idem, but vector is a double precision float
// From fortran it is called as icontrol=idpwritecstdout(vector,nelements).
// Under cygwin, first fix the stdout stream to binary by using fixstd()

int idpwritecstdout_(double *vector, int *nelements)
{
    return fwrite(vector, 8, *nelements, stdout);
}


/******* READING ROUTINES *******************************************/

// reads in binary C from the file fr opened for reading a float
// vector of nelements. It returns the number of elements (nelements) read.
// From fortran it is called as icontrol=ireadcfr(vector,nelements) , with
// vector a single precision float.

int ireadcfr_(float *vector, int *nelements)
{
    if (fr == NULL) {
          fprintf(stderr,"C-binary file is not open to read\n");
          exit(EXIT_FAILURE);
    }
    return fread(vector, 4, *nelements, fr);
}


// idem, but vector is a double precision float.
// From fortran it is called as icontrol=idpreadcfr(vector,nelements).

int idpreadcfr_(double *vector, int *nelements)
{
    if (fr == NULL){
          fprintf(stderr,"C-binary file is not open to read\n");
          exit(EXIT_FAILURE);
    }
    return fread(vector, 8, *nelements, fr);
}

// reads in binary C from stdin a float
// vector of nelements. It returns the number of elements (nelements) read.
// From fortran it is called as icontrol=ireadcstdin(vector,nelements).
// Under cygwin, first fix the stdin stream to binary by using fixstd()

int ireadcstdin_(float *vector, int *nelements)
{
    return fread(vector, 4, *nelements, stdin);
}

// idem, but vector is a double precision float.
// From fortran it is called as icontrol=idpreadcstdin(vector,nelements).
// Under cygwin, first fix the stdin stream to binary by using fixstd()

int idpreadcstdin_(double *vector, int *nelements)
{
    return fread(vector, 8, *nelements, stdin);
}

/******* The following are non-ANSI C functions   *****************/

// the same as iwritecstdout , but using write instead of fwrite. nbytes is the
// number of bytes, i.e.  4*nelements. It returns the number of bytes written.
// From fortran it is called as icontrol=idumpc(vector,nbytes).
// Under cygwin, first fix the stdout stream to binary by using fixstd()

int idumpc_(float *vector, int *nbytes)
{
    // 0 is stdin, 1 is stdout, 2 is stderr
    return( write(1, vector, *nbytes) );
}

// idem, but vector is double precision. nbytes is the
// number of bytes, i.e.  8*nelements. It returns the number of bytes written.
// From fortran it is called as icontrol=idpdumpc(vector,nbytes).
// Under cygwin, first fix the stdout stream to binary by using fixstd()

int idpdumpc_(double *vector, int *nbytes)
{
    // 0 is stdin, 1 is stdout, 2 is stderr
    return( write(1,vector,*nbytes) );
}


// the same as ireadcstdin , but using read instead of fread. nbytes is the
// number of bytes, i.e.  4*nelements. It returns the number of bytes read.
// From fortran it is called as icontrol=ireadc(vector,nbytes).
// Under cygwin, first fix the stdin stream to binary by using fixstd()

int ireadc_(float *vector, int *nbytes)
{
    // 0 is stdin, 1 is stdout, 2 is stderr
    return( read(0, vector, *nbytes) );
}

// idem, but vector is double precision. nbytes is the
// number of bytes, i.e.  8*nelements. It returns the number of bytes read.
// From fortran it is called as icontrol=idpreadc(vector,nbytes).
// Under cygwin, first fix the stdin stream to binary by using fixstd()

int idpreadc_(double *vector, int *nbytes)
{
    // 0 is stdin, 1 is stdout, 2 is stderr
    return( read(0, vector, *nbytes) );
}

// void fixstd_(void)
// use this only under cygwin (unix-like environment for windows) and only before
// using iwritecstdout and/or ireadcstdin (or ireadc or idumpc) redirected to local files.
// It changes the default mode from text to binary.
// It will do nothing under other circumstances (but O_BINARY will be undefined in most
// non-cygwin environments, so comment out this function when compiling there).

#ifdef O_BINARY
void fixstd_(void)
{
    // 0 is stdin, 1 is stdout, 2 is stderr */
    setmode(1,O_BINARY);
    setmode(0,O_BINARY);
}
#endif
热门问题
推荐问题
最新问题