`Error: Unexpected NAMELIST statement` in GNUFortran

`Error: Unexpected NAMELIST statement` in GNUFortran

以下脚本创建派生类型,然后尝试将其名单输出到文本文件:

program test
    implicit none
    character(len=:), allocatable :: CurrentString
    integer :: linelength, IO

    type SubjectType
            character(20) :: genre
            character(20) :: maindude
    end type SubjectType

    type BookType
            character(20) :: title
            character(20) :: author
            type(SubjectType) :: subject
            integer, dimension(2,2):: array
            integer :: BookID

    end type Booktype

    type(Booktype) :: Book

    Book%title = "Harry Potter"
    Book%author = "JK Rowling"
    Book%subject%genre = "Fantasy"
    Book%subject%maindude = "Ron Weasley"
    Book%array = RESHAPE([1,2,3,4],[2,2])
    Book%BookID = 105
    open(10, file = 'namelist.txt')

    namelist /mynamelist/ Book
    write(10, nml = mynamelist)
    close(10, status = 'keep')
end program test

但是编译时会抛出错误:

||=== Build: Debug in Hello (compiler: GNU Fortran Compiler) ===|

D:\TEMP\Hello\main.f95|30|Error: Unexpected NAMELIST statement |

D:\TEMP\Hello\main.f95|31|Error: Symbol 'mynamelist' must be a

NAMELIST group name| ||=== Build failed: 2 error(s), 0 warning(s) (0

minute(s), 2 second(s)) ===|

一个google search真的不是很有帮助。我找到的唯一论坛讨论了为什么会发生这种情况,但没有讨论如何解决它。我怎样才能在 GFortran 中将其设置为 运行?请注意这在 IFort 中确实有效。

NAMELIST 是声明语句。它不能放在可执行语句之间。只能用在第一个可执行语句之前的每个单元的开头。

 type(Booktype) :: Book

 namelist /mynamelist/ Book

 Book%title = "Harry Potter"

 ...

 open(10, file = 'namelist.txt')
 write(10, nml = mynamelist)
 close(10, status = 'keep')