Fortran模块需要位于另一个模块中的子例程

时间:2017-07-10 08:55:01

标签: fortran gfortran fortran90 intel-fortran

我遇到这样的问题:

  1. main.f90 - >包含MAIN文件
  2. sub_A.f90 - >包含子程序A
  3. sub_B.f90 - >包含子程序B
  4. other_stuffs.f90 - >包含第2点和第3点所需的所有功能。
  5. 所有四点都是单独写的。

    main.f90时

    include sub_A.f90
    include sub_B.f90
    include other_stuffs.f90
    
    program MAIN
    use A
    use B
    use other
         ...
         call proc_A   
         call_proc_B
    end program MAIN
    

    sub_A.f90

    module A
    contains
        subroutine proc_A
        use other            
        ...
            call compute_something_1
        end subroutine proc_A
    end module A
    

    sub_B.f90

    module B
    contains  
        subroutine proc_B
        use other            
        ...
            call compute_something_2
        end subroutine proc_B
    end module B
    

    other_stuffs.f90

    module other
    contains  
        subroutine compute_something_1
        ...
        end subroutine compute_something_1
    
        subroutine compute_something_2
        ...
        end subroutine compute_something_2
    end module other
    

    不幸的是,它没有用。我做错什么了吗?

2 个答案:

答案 0 :(得分:2)

您错过了contains模块中的other部分。 只能从模块外部访问contains部分中的子程序,函数和变量。

module other

contains  
    subroutine compute_something_1
    ...
    end subroutine compute_something_1

    subroutine compute_something_2
    ...
    end subroutine compute_something_2
end module other

答案 1 :(得分:0)

答案应如下

<强> main.f90时

IIS_IUSRS ([server name]\IIS_IUSRS)

<强> sub_A.f90

include other_stuffs.f90     !It becomes the first now
include sub_A.f90
include sub_B.f90

program MAIN
use other
use A
use B
     ...
     call proc_A   
     call_proc_B
end program MAIN

<强> sub_B.f90

module A
use other     ! CORRECT PLACE
contains
    subroutine proc_A
    ! use other             !WRONG PLACE !!!
    ...
        call compute_something_1
    end subroutine proc_A
end module A

<强> other_stuffs.f90

module B
use other     ! CORRECT PLACE
contains  
    subroutine proc_B
    ! use other             !WRONG PLACE !!!
    ...
        call compute_something_2
    end subroutine proc_B
end module B

谢谢大家。