旧Fortran计划:ld返回1退出状态

时间:2013-08-09 20:50:00

标签: linux linker-errors ld gfortran fortran77

我有一个旧的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

1 个答案:

答案 0 :(得分:2)

编辑:问题显然不是来自缺乏程序(抱歉,我没有醒来: - ))。

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

然而:

  • 有许多警告(未使用的变量或格式)
  • ACCEPT应该是equivalent to 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])