You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

gfortran中BLOCK结构与OpenMP联用编译错误问题咨询

OpenMP并行块中带名称的BLOCK语句编译错误问题

问题场景

用户编写的Fortran程序如下:

program my_pgm

    use omp_lib
    implicit none
        
    CALL OMP_set_dynamic(.FALSE.)
    CALL OMP_set_num_threads(2)
    
    !$OMP PARALLEL DEFAULT(SHARED)
    
    my_parallel_block : BLOCK

        integer :: my_idx
 
        DO my_idx = 0, 3
            WRITE(*,*) 'my_idx , thread_num, ',my_idx,omp_get_thread_num()
        END DO
    
    END BLOCK my_parallel_block
    
    !$OMP END PARALLEL
    
end program my_pgm

使用命令gfortran -fopenmp mypgm.f90编译时,出现以下错误:

fortranblock.f90:19:31:

19 | END BLOCK my_parallel_block
| 1 Error: Syntax error in END BLOCK statement at (1)
fortranblock.f90:21:22:

21 | !$OMP END PARALLEL
| 1 Error: Unexpected !$OMP END PARALLEL statement at (1)
fortranblock.f90:23:3:

23 | end program my_pgm
| 1 Error: Expecting END BLOCK statement at (1)
f951: Error: Unexpected end of file in 'fortranblock.f90'

用户发现将END BLOCK my_parallel_block修改为END BLOCK !my_parallel_block(注释掉块名)可避免错误,现咨询两个问题:

  • 该问题是否为GNU Fortran编译器特有?
  • 是否可通过编译选项保留块名?

问题解答

1. 是否为gfortran特有问题?

这个错误确实是gfortran旧版本的特有兼容性问题。在处理OpenMP并行指令与Fortran命名BLOCK结构的交互时,gfortran 10.x之前的版本存在语法解析缺陷,无法正确识别并行区域内带名称的END BLOCK语句。而Intel Fortran、NAG Fortran等其他主流编译器,对这种符合Fortran标准的语法支持正常,不会出现此类错误。

2. 能否通过编译选项保留块名?

  • 若使用gfortran 10.x及以上版本,该问题已被官方修复,无需额外编译选项,直接执行gfortran -fopenmp mypgm.f90即可正常编译带名称的BLOCK结构。
  • 若无法升级编译器,目前没有专门的编译选项能让旧版gfortran兼容这种写法,只能通过两种方式规避:
    • 如用户尝试的,注释掉END BLOCK后的块名(END BLOCK !my_parallel_block)
    • 删除BLOCK结构的名称,直接写END BLOCK

内容的提问来源于stack exchange,提问作者Stef1611

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 13:05:04