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

Reading columns from a file in Fortran, g++ gives `Undefined symbols: "__gfortran_set_args"`

我正在尝试从 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

正如@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