混淆了Fortran子模块和gcc compliation标志-Wuse-without-only

时间:2017-01-25 00:07:59

标签: oop gcc module fortran gfortran

此gcc Fortran编译警告的补救措施是什么?

USE statement at (1) has no ONLY qualifier

在gcc 6.0,6.1,6.2和7.0中使用子模块时会出现警告。

完整的编译顺序和警告:

$ gfortran -c -Wuse-without-only -o mod_module.o mod_module.f08
$ gfortran -c -Wuse-without-only -o mod_module_sub.o mod_module_sub.f08
mod_module_sub.f08:1:19:

 submodule ( mModule ) mSubModule
                   1
Warning: USE statement at (1) has no ONLY qualifier [-Wuse-without-only]
$ gfortran -c -Wuse-without-only -o demonstration.o demonstration.f08
$ gfortran  -o demonstration demonstration.o mod_module.o mod_module_sub.o
$ ./demonstration 
 this + that =    3.00000000    
 expected value is 3

主程序(demonstration.f08):

program demonstration
    use mModule, only : myType
    implicit none
    type ( myType ) :: example
        example % this = 1.0
        example % that = 2.0
        call example % adder ( )
        write ( *, * ) 'this + that = ', example % other
        write ( *, * ) 'expected value is 3'
    stop
end program demonstration

模块(mod_module.f08):

module mModule
    implicit none
    type :: myType
        real :: this, that, other
    contains
        private
        procedure, public :: adder => adder_sub
    end type myType

    private :: adder_sub

    interface
        module subroutine adder_sub ( me )
            class ( myType ), target :: me
        end subroutine adder_sub
    end interface

end module mModule

子模块(mod_module_sub.f08):

submodule ( mModule ) mSubModule  ! <=== problematic statement
    implicit none
contains
    module subroutine adder_sub ( me )
        class ( myType ), target :: me
        me % other = me % this + me % that
    end subroutine adder_sub
end submodule mSubModule

也就是说,指定子模块的正确方法是什么?标志-Wuse-without-only对于编制较长的代码至关重要。

1 个答案:

答案 0 :(得分:2)

根据您的观点,它只是一个编译器错误。提交错误报告并等待其修复(或自行修复)。

(另一种观点是,因为该代码使子模块能够访问其主机的所有实体,无论是否需要,警告都是适当的。但限制主机关联需要F2015支持。)

-Wuse-without-only只是一个警告,有助于强制执行特定的编程风格(我认为这种风格特别有用)。它不可能是必不可少的&#34;编译任何代码,短或长。如果在此期间警告困扰您,请删除该选项。