【问题标题】:In Mac gfortran 4.7.2, isatty(6) does not work在 Mac gfortran 4.7.2 中,isatty(6) 不起作用
【发布时间】:2013-01-17 00:32:23
【问题描述】:

在 Mac 10.8.2 中,使用 gfortran 4.7.2 编译如下代码:

program test
    write(*, *) isatty(6)
end program test

程序只是不响应也不返回。有什么想法吗?

【问题讨论】:

  • 你真的需要 isatty() 调用吗,它是 Fortran 的 GNU 扩展?如果您的目的是获取标准输出的单元号,您可以使用内部模块iso_fortran_env 中的参数output_unit。
  • 是的,我需要它。因为我需要判断程序输出是否重定向到文件。我在输出上添加了一些颜色代码,所以如果重定向,就会出现丑陋的颜色代码。

标签: fortran gfortran


【解决方案1】:

我不太确定你为什么没有输出,在 Linux 上使用 gfortran 4.7.2 对我来说效果很好。您可以尝试为 C 的 isatty 编写自己的接口是否有任何不同:

program test
  implicit none

  interface
    function my_isatty(fd) bind(C, name = 'isatty')
      use, intrinsic :: iso_c_binding, only: c_int
      integer(c_int)        :: my_isatty
      integer(c_int), value :: fd
    end function
  end interface

  ! Standard output should be at 1 in C:
  print*, my_isatty(1) 
end program test

我得到以下输出:

$ ./a.out
    1
$ ./a.out > b && cat b
    0

【讨论】:

  • 它适用于带有 gfortran 4.7.2 的 Mac 10.8.2!谢谢!这可能是 gfortran 中的错误?
  • 好吧,我不知道 gfortran 到底是做什么的;我希望它调用与我的答案相同的 C 函数。您也可以咨询 gfortran 开发人员,这可能是一个错误。
  • 显然gfortran的isatty来自自己的库,C接口直接调用C库函数。我在程序的符号表中得到的条目分别是_gfortran_isatty_l4@@GFORTRAN_1.0 和isatty@@GLIBC_2.2.5。
猜你喜欢
  • 2020-01-11
  • 1970-01-01
  • 2013-05-08
  • 2014-01-29
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
相关资源
最近更新 更多