3

我有一个旧的 FORTRAN 77 程序,我无法正常编译/构建: gfortran -Wall -o "filename" filename.f

它不断给我链接器错误:

$ gfortran -Wall  ljewald.f 

/usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status

最终,我尝试了:gfortran -Wall -c -o "filename" filename.f这给了我编译的二进制文件。好的,但 gfortran 的手册页正在勾勒我。这是 -c 选项的材料,它使这一切看起来都有效:

   -C  Do not discard comments. All comments are passed through to the output file, except for comments in processed directives, which are deleted
       along with the directive.

       You should be prepared for side effects when using -C; it causes the preprocessor to treat comments as tokens in their own right. For example,
       comments appearing at the start of what would be a directive line have the effect of turning that line into an ordinary source line, since the
       first token on the line is no longer a '#'.

       Warning: this currently handles C-Style comments only. The preprocessor does not yet recognize Fortran-style comments.

因此,在构建此之后,使用: gfortran -Wall -c -o "ljewald" ljewald.f

我得到一个输出文件,但它不是可执行文件......?

$ls -l
...
-rw-rw-r--  1 j0h j0h    647 Aug  9 16:36 ljewald 
...

即使我使用 chmod +x ljewald 更改模式,我也无法执行此文件

我该怎么做才能避免使用 -c 选项,因为使用它有怪癖?以及如何构建该程序的可执行文件?有人可以解释一下,并告诉我如何解决这个问题:?

/usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status

链接到来源: http: //nanocluster.umeche.maine.edu/ljewald.f

4

1 回答 1

2

编辑:问题显然不是来自缺少程序(对不起,我没醒:-))。

实际上,行尾导致了问题:在 Windows 上正确转换为 CRLF 时,gcc-4.8.1 成功编译(在注释掉 ACCEPT 之后)。

然而:

  • 有很多警告(未使用的变量或格式)
  • ACCEPT 应该等同于 READ,然后标签 777 丢失(对于 READ 格式)
  • 有一些带有表格的行,确实应该避免,尤其是当几乎所有代码都用空格缩进时。

如果您有权访问 Windows 框,则可以使用 Notepad++ 转换行尾并替换制表符。

如果您有很多文件要修复,您可以尝试使用 python 脚本。您可以详细说明以下内容,我经常根据给定的规则来清理文件(您可以根据需要更改cleanfile函数,这里它转换为 CRLF,并删除无用的空白)。它在 Python 3 中,但如果需要,很容易转换为 Python 2。

# encoding: ISO-8859-15

import sys, os, hashlib

def filehash(name):
   f = open(name, "rb")
   h = hashlib.sha512()
   n = 4 * 1024 * 1024
   while True:
      r = f.read(n)
      h.update(r)
      if len(r) < n:
         break
   f.close()
   return h.hexdigest()

def cleanfile(name):
   v = os.stat(name)
   a = filehash(name)
   atime = v[7]
   mtime = v[8]
   f = open(name, "rt", encoding="ISO-8859-1")
   u = f.readlines()
   f.close()

   n = len(u)
   for i in range(n):
      u[i] = u[i].rstrip()

   while n > 0 and u[n - 1] == "":
      n -= 1

   if n == 0:
      print("EMPTY FILE {}".format(name))
      os.remove(name)
      return

   #f = open(name, "wt", newline="\n")
   f = open(name, "wt", encoding="ISO-8859-1")
   for i in range(n):
      s = u[i]
      f.write("{}\n".format(s))
   f.close()

   os.utime(name, (atime, mtime))
   b = filehash(name)
   if a != b:
      print("MODIF {}".format(name))

def manfile(name):
   global exts
   n = name.rfind(".")
   if n < 0:
      print("PASS {}".format(name))
   e = name[n + 1:].lower()

   if e in ["f"]:
      cleanfile(name)
   else:
      print("SKIP {}  -  {}".format(e, name))


########### recursive directory traversal, don't whange after this line ###########

def mandir(path):
   try:
      li = os.listdir(path)
   except:
      print("ERRD {}".format(path))
      return
   li.sort()
   lilnk = [ ]
   lifil = [ ]
   lidir = [ ]
   for name in li:
      c = os.path.join(path, name)
      if os.path.islink(c):
         lilnk.append(c)
      elif os.path.isfile(c):
         lifil.append(c)
      elif os.path.isdir(c):
         lidir.append(c)
      else:
         print("UNKN {}".format(c))
   for c in lilnk:
      os.remove(c)
      pass
   for c in lifil:
      manfile(c)
   for c in lidir:
      mandir(c)
   li = os.listdir(path)
   if len(li) == 0:
      try:
         os.rmdir(path)
      except OSError:
         pass

mandir(sys.argv[1])
于 2013-08-13T07:54:49.710 回答