从Fortran中的文件中读取列,g ++给出了“未定义的符号:”__ gfortran_set_args“`

时间:2017-08-10 14:30:30

标签: fortran fortran90

我正在尝试从Fortran的输入文件中读取列,以便将它们用于其他计算。

当我用g ++编译时,我读到了这个错误:

 Undefined symbols for architecture x86_64:
  "__gfortran_set_args", referenced from:
      _main in ccOO2MBV.o
  "__gfortran_set_options", referenced from:
      _main in ccOO2MBV.o
  "__gfortran_st_close", referenced from:
      _MAIN__ in ccOO2MBV.o
  "__gfortran_st_open", referenced from:
      _MAIN__ in ccOO2MBV.o
  "__gfortran_st_read", referenced from:
      _MAIN__ in ccOO2MBV.o
  "__gfortran_st_read_done", referenced from:
      _MAIN__ in ccOO2MBV.o
  "__gfortran_transfer_real", referenced from:
      _MAIN__ in ccOO2MBV.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我哪里错了?代码是这样的:

program columns

  INTEGER,SAVE :: lun
  INTEGER, PARAMETER :: ARRAYLEN=1440
  CHARACTER :: filename
  DOUBLE PRECISION, DIMENSION (1044) :: X_halo, Y_halo, Z_halo
  INTEGER :: i

  lun=1
  filename = 'xyz.dat'

  OPEN (1, FILE='xyz.dat',STATUS='old', ACTION='read', iostat=istat)

    do i=1,1440
       READ (1, iostat=istat) X_halo(i), Y_halo(i), Z_halo(i)
    end do

  CLOSE (1)

end program columns

1 个答案:

答案 0 :(得分:2)

正如@ d_1999指出的那样,编译器应该是gfortran,而不是g++

除此之外,将@Ross的评论更改为答案,您的代码应以指定的READ格式运行,此处为

READ (1, *, iostat=istat) X_halo(i), Y_halo(i), Z_halo(i)

提供使代码运行所需的最小更改。除此之外,还要看看这里实现的其他差异:

program columns
  ! Add implicit none to catch that `istat` is not declared
  IMPLICIT NONE

  INTEGER,SAVE :: lun
  INTEGER, PARAMETER :: ARRAYLEN=1440
  ! Make `filename` bigger than a single character
  CHARACTER(120) :: filename
  ! can add `ARRAYLEN` here
  DOUBLE PRECISION, DIMENSION (ARRAYLEN) :: X_halo, Y_halo, Z_halo
  ! Have added `istat` here
  INTEGER :: i, istat

  lun=1
  filename = 'xyz.dat'
  ! Have replaced `xyz.dat` with `filename` and using a higher `UNIT` number
  OPEN (UNIT=10, FILE=filename, STATUS='old', ACTION='read', IOSTAT=istat)

    ! Using `ARRAYLEN` for the loop.
    ! I've also capitalised the keywords (matter of preference)
    DO i=1,ARRAYLEN
       ! And the important format specifier
       READ (10, *, iostat=istat) X_halo(i), Y_halo(i), Z_halo(i)
    END DO

  CLOSE (10)

end program columns

其中一些问题(例如filename不够大)将通过使用-Wall标记进行编译来捕获,例如

之类的东西
gfortran -Wall columns.f90 -o columns.exe