Fortran标准与内在过程标准
在GNU Fortran编译器的文档中,对于特定的内在函数,有一个标准。例如,Fortran标准与内在过程标准,fortran,Fortran,在GNU Fortran编译器的文档中,对于特定的内在函数,有一个标准。例如,CPU\u TIME是一个内在过程,它表明标准为Fortran 95及更高版本。我的问题是关于这个的意义 我理解Fortran标准就像一套规则,您的代码将被定义为符合标准(Fortran 77、90、952003或2008)。这是否意味着CPU\u TIME只能与Fortran 95+一起使用?实际上,我已经知道答案了,我可以在扩展名为.f的Fortran 77文件中使用CPU\u时间,而不用编译器抱怨,使用Gfort
CPU\u TIME
是一个内在过程,它表明标准为Fortran 95及更高版本。我的问题是关于这个的意义
我理解Fortran标准就像一套规则,您的代码将被定义为符合标准(Fortran 77、90、952003或2008)。这是否意味着
CPU\u TIME
只能与Fortran 95+一起使用?实际上,我已经知道答案了,我可以在扩展名为.f的Fortran 77文件中使用CPU\u时间
,而不用编译器抱怨,使用Gfortran版本>5编译。这是因为编译器能够处理代码中遇到的任何标准吗?我知道-std
标志,但如何确保例如Fortran 77代码只使用Fortran 77的内部过程?简短回答,您无法用gnu编译器区分f95以下的标准。从F95和以上,您可以使用STD选项强制编译器将上述标准的特性视为错误。
长答覆:
报告说:
-标准=标准
指定程序应符合的标准,可以是“f95”、“f2003”、“f2008”、“gnu”或“遗留”中的一种。这个
std的默认值为“gnu”,它指定
Fortran 95标准,包括
GNU Fortran,但对于过时的扩展将发出警告
不建议在新代码中使用。“遗留”值是等效的
但是没有过时扩展的警告,并且可能是有用的
对于旧的非标准程序。“f95”、“f2003”和“f2008”值
指定与Fortran 95、Fortran 2003和Fortran的严格一致性
分别为2008年标准;所有扩展都会出现错误
超出相关语言标准,并针对以下情况发出警告:
Fortran 77允许但在以后将被淘汰的功能
标准。”-std=f2008ts'允许Fortran 2008标准,包括
技术规范(TS)29113的新增内容包括:
Fortran与C和TS 18508在附加并行机上的互操作性
Fortran中的功能
您可能需要检查其他编译器
易于验证:,编译以下程序(由提供),使用和不使用选项-std=f95
,然后查看发生了什么
module class_Circle
implicit none
private
real :: pi = 3.1415926535897931d0 ! Class-wide private constant
type, public :: Circle
real :: radius
contains
procedure :: area => circle_area
procedure :: print => circle_print
end type Circle
contains
function circle_area(this) result(area)
class(Circle), intent(in) :: this
real :: area
area = pi * this%radius**2
end function circle_area
subroutine circle_print(this)
class(Circle), intent(in) :: this
real :: area
area = this%area() ! Call the type-bound function
print *, 'Circle: r = ', this%radius, ' area = ', area
end subroutine circle_print
end module class_Circle
program circle_test
use class_Circle
implicit none
type(Circle) :: c ! Declare a variable of type Circle.
c = Circle(1.5) ! Use the implicit constructor, radius = 1.5.
call c%print ! Call the type-bound subroutine
end program circle_test
有一件事我认为你很困惑,那就是.f和.f90文件的含义。文件扩展名仅指源代码的格式-自由格式或固定格式。键入gfortran main.f并不意味着它使用的是F77编译器。@罗斯:我知道自由格式/固定格式以及文件的扩展名,但我认为键入
gfortran main.f
编译的是Fortran 77代码。我很难理解编译器实际编译的Fortran(77、90等)类型的区别以及与标准的关系。如果您需要与旧的遗留编译器兼容,最可靠的检查将是使用该编译器进行验证。否则,我想知道您为什么关心保持与一些旧标准的一致性。不,默认情况下,gfortran编译.f
文件为Fortran 2008。和.f90
也与Fortran 2008一样。源代码形式不同,但使用的标准不同。Ad 1)Gfortran为Fortran 95及更早版本的任何内容编写Fortran 95。有充分的理由。忘记Fortran 90吧,它不值得。没有人用它。Fortran 95已经完全取代了它。Fortran 90有几个缺点和严重的问题,由Fortran 95修复。忘掉Fortran 90吧。Gfortran也不考虑这个问题。我不知道它是在90年还是95年推出的,我也不在乎。
module class_Circle
implicit none
private
real :: pi = 3.1415926535897931d0 ! Class-wide private constant
type, public :: Circle
real :: radius
contains
procedure :: area => circle_area
procedure :: print => circle_print
end type Circle
contains
function circle_area(this) result(area)
class(Circle), intent(in) :: this
real :: area
area = pi * this%radius**2
end function circle_area
subroutine circle_print(this)
class(Circle), intent(in) :: this
real :: area
area = this%area() ! Call the type-bound function
print *, 'Circle: r = ', this%radius, ' area = ', area
end subroutine circle_print
end module class_Circle
program circle_test
use class_Circle
implicit none
type(Circle) :: c ! Declare a variable of type Circle.
c = Circle(1.5) ! Use the implicit constructor, radius = 1.5.
call c%print ! Call the type-bound subroutine
end program circle_test