这个 Fortran '77 代码片段有什么问题?

发布于 2024-09-02 03:01:01 字数 1185 浏览 3 评论 0 原文

我的任务是维护一些遗留的 Fortran 代码,但我在使用 gfortran 编译它时遇到了麻烦。我已经编写了大量的 Fortran 95,但这是我第一次使用 Fortran 77。这段代码是有问题的:

      CHARACTER*22 IFILE, OFILE
      IFILE='TEST.IN'
      OFILE='TEST.OUT'
      OPEN(5,FILE=IFILE,STATUS='NEW')   
      OPEN(6,FILE=OFILE,STATUS='NEW')
      common/pabcde/nfghi

当我使用 gfortran file.FOR 进行编译时,所有以common 语句是错误(例如,对于以下每一行,Error: Unexpected COMMON statements at (1),直到达到 25 个错误限制)。我使用 -Wall -pedantic 进行编译,但修复警告并没有解决这个问题。

疯狂的是,如果我注释掉以 IF='TEST.IN' 开头的所有 4 行,程序就会按预期编译并运行,但是我必须全部注释掉。如果不注释它们中的任何一个,就会出现从 common 语句开始的相同错误。如果我注释掉 common 语句,我会得到相同的错误,只是从下一行开始。

我在 OS X Leopard(不是 Snow Leopard)上使用 gfortran。我已经广泛使用这个系统和 gfortran 来编写 Fortran 95 程序,因此理论上编译器本身是正常的。这段代码到底是怎么回事?


编辑:使用 g77 编译给出:

test.FOR: In program `MAIN__':
test.FOR:154: 
     IFILE='TEST.IN'
     1
test.FOR:158: (continued):
     common/pabcde/nfghi
     2
Statement at (2) invalid in context established by statement at (1)

呃,(1) 处建立了什么上下文?

I've been tasked with maintaing some legacy fortran code, and I'm having trouble getting it to compile with gfortran. I've written a fair amount of Fortran 95, but this is my first experience with Fortran 77. This snippet of code is the problematic one:

      CHARACTER*22 IFILE, OFILE
      IFILE='TEST.IN'
      OFILE='TEST.OUT'
      OPEN(5,FILE=IFILE,STATUS='NEW')   
      OPEN(6,FILE=OFILE,STATUS='NEW')
      common/pabcde/nfghi

When I compile with gfortran file.FOR, all lines starting with the common statement are errors (e.g. Error: Unexpected COMMON statement at (1) for each following line until it hits the 25 error limit). I compiled with -Wall -pedantic, but fixing the warnings did not fix this problem.

The crazy thing is that if I comment out all 4 lines starting with IF='TEST.IN', the program compiles and works as expected, but I must comment out all of them. Leaving any of them uncommented gives me the same errors starting with the common statement. If I comment out the common statement, I get the same errors, just starting on the following line.

I am on OS X Leopard (not Snow Leopard) using gfortran. I've used this very system with gfortran extensively to write Fortran 95 programs, so in theory the compiler itself is sane. What the hell is going on with this code?


Edit: Compiling with g77 gives:

test.FOR: In program `MAIN__':
test.FOR:154: 
     IFILE='TEST.IN'
     1
test.FOR:158: (continued):
     common/pabcde/nfghi
     2
Statement at (2) invalid in context established by statement at (1)

Er, what context is established at (1)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-09-09 03:01:01

我认为您不能将 COMMON 语句放在 FORTRAN 77 中的可执行语句下面,请参阅 规范,第 2 节3.5.
只需将 COMMON 语句移至靠近过程开头、任何可执行语句之前即可。

I don't think you can put COMMON statements below the executable statements in FORTRAN 77, see the specification, Sec. 3.5.
Just move the COMMON statement close to the beginning of the procedure, before any executable statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文