2017-07-18 47 views
2

我想端口一段代碼的Fortran 77到Fortran 90的,我有關於Fortran 77中追趕排名不匹配參數等級不匹配的沒有報告

這是一個問題在Fortran 90的

program test 
use my_module 
real   ml_time 
call gettimes(cdfid,ml_time,ml_ntimes) 

代碼在調用子例程,這是通過可變如何定義

module my_module 
use netcdf 

subroutine gettimes(cdfid,times,ntimes) 
real times(*) 

    call check(nf90_inq_dimid(cdfid,'time', timid)) 

    call check(nf90_inquire_dimension(cdfid, timid, len = ntimes)) 

    call check(nf90_inq_varid(cdfid,'time',timid)) 

    call check(nf90_get_var(cdfid,timid,times(1:ntimes))) 


end subroutine gettimes 

在Fortran 77的(.f文件)和gfortran 5.4爲什麼這個無會產生編譯錯誤?

將代碼移植到Fortran 90時出現相同的代碼會產生排名不匹配的編譯錯誤。

這是在Fortran 77的Fortran 90中

add2p.f90:191:22: 

call gettimes(cdfid,ml_time,ml_ntimes) 
        1 
Error: Rank mismatch in argument ‘times’ at (1) (rank-1 and scalar) 

錯誤這是代碼是如何組織的

program test 
real   ml_time 
call gettimes(cdfid,ml_time,ml_ntimes) 

在另一個文件xyz.f

subroutine gettimes(cdfid,times,ntimes,ierr) 

    include "netcdf.inc" 

    integer ierr,i 
    real times(*) 
    integer didtim,ntimes 

    integer cdfid,idtime 


    do 10 i=1,ntimes 
    call ncvgt1(cdfid,idtime,i,times(i)) ! get times 
    10 continue 




    end 

當然我擺脫了錯誤,使他們相同的排名,但我想知道爲什麼在Fortran 77中沒有報告編譯器錯誤。

+0

請顯示在Fortran 90中生成的錯誤以及一些合理的*完整*代碼示例[mcve]。 –

+0

整個結構是需要的,不僅僅是子程序,看到我的答案爲什麼。 –

+0

@VladimirF - 我做出了可接受的更改嗎? – gansub

回答

2

您沒有顯示足夠的代碼,但您可能在Fortran 90代碼中使用顯式接口(例如模塊)。在這種情況下,編譯器有義務檢查這種不一致性並且必須產生錯誤。當使用隱式接口時,情況並非如此(它們在Fortran 77中沒有明確的接口)。

僅當標量是數組元素時才允許將標量傳遞給假定的大小數組(參見序列關聯)。

我得到在gfortran 4.8警告,但如果呼叫處於不同的源文件,該文件可能不會發生:

subroutine s1(a) 
    integer :: a(*) 
    end 

    subroutine s2() 
    call s1(1) 
    end subroutine 

> gfortran rank.f90 -c 
rank.f90:7.12: 

    call s1(1) 
      1 
Warning: Rank mismatch in argument 'a' at (1) (rank-1 and scalar) 

需要注意的是編譯器編譯所有的源代碼的Fortran 2008 +默認擴展。它不以任何方式區分Fortran 90和77。

值得注意的是,.f和.f90並不意味着Fortran 77和Fortran 90,它們是指固定格式和自由格式的源文件。這兩種源表格都是有效的Fortran 90 - Fortran 2008.

+0

你是怎麼猜測我在使用模塊的?這真是太神奇了!向你致敬。 – gansub

+0

@VlaldimirF - 是的調用函數和調用的函數在Fortran 77中有兩個不同的文件。另一個好的捕獲。順便說一下,現在我下一次問一個問題,我知道要包含哪些細節才能構成一個完整的例子。感謝您指出了這一點 ! – gansub

+0

所以在Fortran 77中,標量是如何轉換成矢量的,反之亦然,或者是一個單獨的問題?:) – gansub