Linux   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了linux – Gfortran警告抱怨“Wmaybe -ininitialized”大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我最近开发了一个相当长的Fortran代码.我正在使用的编译器是在Opensuse 13.1(64位)上的gfortran 4.8.1.但是当我使用-O2或-O3选项编译代码时,我收到很多关于“-Wmaybe -ininitialized”的警告.我设法将代码减少到最小的工作示例,如下所示. 在main.f90中 program main use modTest implicit
我最近开发了一个相当长的Fortran代码.我正在使用的编译器是在Opensuse 13.1(64位)上的gfortran 4.8.1.但是当我使用-O2或-O3选项编译代码时,我收到很多关于“-Wmaybe -ininitialized”的警告.我设法将代码减少到最小的工作示例,如下所示.

在main.f90中

program main
    use modTest
    implicit none

    real(kind = 8),dimension(:,:),allocatable :: output
    real(kind = 8),:,allocatable :: input

    allocate(input(22,33,20),output(22,33))
    input = 2.0
    call test(input,output)

end program main

在test.f90中

@H_21_23@module modTest contains subroutIne test(inputValue,outvalue) use modGlobal implicit none real(kind = 8),intent(in) :: inputValue real(kind = 8),intent(out) :: outValue Integer :: nR,nX,nM,iM,ALLOCATESTATUS real,allocatable :: cosMPhi nR = size(inputValue,1) nX = size(inputValue,2) nM = size(inputValue,3) - 1 allocate(cosMPhi(nR,0:nM),stat=ALLOCATESTATUS) call checkStatus(ALLOCATESTATUS) do iM = 0,nM cosMPhi(:,iM) = cos(iM * 1.0) end do outValue = sum(inputValue * cosMPhi,3) end subroutIne end module

在global.f90中

@H_21_23@module modGlobal contains subroutIne checkStatus(stat) implicit none Integer,intent(in) :: stat if(stat /= 0) then print *,"alLOCATIOn Failed" stop end if end subroutIne end module

使用gfortran -O2 -Wall test.f90 main.f90 -o run编译时,会出现以下警告:

test.f90: In function 'test':
test.f90:9:0: warning: 'cosmphi.dim[2].Stride' may be used uninitialized in this function [-Wmaybe-uninitialized]
     real,allocatable :: cosMPhi
^
test.f90:9:0: warning: 'cosmphi.dim[1].ubound' may be used uninitialized in this function [-Wmaybe-uninitialized]
test.f90:9:0: warning: 'cosmphi.dim[1].Stride' may be used uninitialized in this function [-Wmaybe-uninitialized]
test.f90:9:0: warning: 'cosmphi.dim[0].ubound' may be used uninitialized in this function [-Wmaybe-uninitialized]
test.f90:9:0: warning: 'cosmphi.offset' may be used uninitialized in this function [-Wmaybe-uninitialized]

我试图谷歌这个问题一段时间,但我仍然没有找到一个好的答案.一些相关网站是:
(1)https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58410
(2)https://groups.google.com/forum/m/#!topic/comp.lang.fortran/RRYoulcSR1k
(3)GCC -Wuninitialized / -Wmaybe-uninitialized issues

我使用gfortran 4.8.5测试了示例代码,警告仍然存在.@R_801_10950@在代码中做错了吗?任何帮助将不胜感激.提前致谢!

解决方法

是因为你在cosMphi的分配中使用stat = ALLOCATESTATUS,但之后不检查状态变量的值.只是省略.然后,如果分配失败,程序将崩溃 – 这是简单的方法,除非您需要更强大/更复杂的响应.

大佬总结

以上是大佬教程为你收集整理的linux – Gfortran警告抱怨“Wmaybe -ininitialized”全部内容,希望文章能够帮你解决linux – Gfortran警告抱怨“Wmaybe -ininitialized”所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。