包含 IEEE_GET_ROUNDING_MODE 的库的 gfortran 链接器参数

gfortran linker argument for library containing IEEE_GET_ROUNDING_MODE

我正在尝试为区间运算编写一个简单的 Fortran 库作为练习。我想显式设置舍入模式,做一些工作,然后将舍入模式恢复到原来的样子。但是,在使用 gfortrangcc 的 Fortran 前端进行编译时,我无法弄清楚哪个库需要 linked 生成可执行文件。

! get_rounding_mode.f03
! print the rounding mode

program get_rounding_mode
  f = IEEE_GET_ROUNDING_MODE()
  print *,f
end program get_rounding_mode

尝试可能可行的最简单的方法给了我

gfortran get_rounding_mode.f03 
/usr/bin/ld: /tmp/ccTLaxeN.o: in function `MAIN__':
get_rounding_mode.f03:(.text+0x20): undefined reference to `ieee_get_rounding_mode_'
collect2: error: ld returned 1 exit status
Exit 1

我到处寻找 ieee_get_rounding,但我不知道如何将 gfortran 指向 link,因为它似乎已经在 libgfortran .

find /usr/ -exec nm --print-file-name '{}' '+' 2>&1 | grep 'ieee_get_rounding'
/usr/lib/libgfortran.so.5:000000000023edc0 T __ieee_arithmetic_MOD_ieee_get_rounding_mode
/usr/lib/libgfortran.so:000000000023edc0 T __ieee_arithmetic_MOD_ieee_get_rounding_mode

IEEE_GET_ROUNDING_MODE 不是函数。它是一个子程序。你需要做类似

的事情
program get_rounding_mode
   use ieee_arithmetic
   implicit none
   ieee_rounding_type mode
   real x
   if (ieee_support_rounding(x)) then
      call ieee_get_rounding_mode(mode)       ! Get current rounding mode
      call ieee_set_rounding_mode(IEEE_TO_UP) ! Set rounding up
      !
      ! Do your work here!
      !
      call ieee_set_rounding_mode(mode)       ! Reset rounding mode
   end if
 end program get_rounding_mode

糟糕,忘记了 implicit nonex

的声明