混淆 Fortran 子模块和 gcc 编译标志 -Wuse-without-only

Confusion with Fortran submodules and gcc compliation flag -Wuse-without-only

这个 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

主程序(演示.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 在编译较长代码时必不可少。

根据您的看法,这只是一个编译器错误。提交错误报告并等待它得到修复(或自行修复)。

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

-Wuse-without-only 只是一个警告,有助于强制执行特定的编程风格(我认为这不是特别有用)。不能"essential"编译任何代码,无论长短。如果警告同时困扰您,请删除该选项。