Fortran链接错误:使用子模块的未定义引用

时间:2016-03-03 22:28:30

标签: module fortran cray

错误消息:

mod_matrices.o:(.data+0x1790): undefined reference to `allocator_rank_2_sub_'

模块mMatrices(在mod_matrices.f08中)调用子模块smAllocations中的函数allocator_rank_2_sub(在mod_sub_matrices_allocators.f08中)。在将模块mMatrices分解为模块和子模块之前,代码已经工作。

模块:

module mMatrices
    use mPrecisionDefinitions,  only : ip, rp    
    implicit none

    type :: matrices
        real ( rp ), allocatable :: A ( : , : ), AS ( : , : ), ASAinv ( : , : )
    contains
        private
        procedure, nopass, public :: allocator_rank_2   => allocator_rank_2_sub
        procedure, public         :: construct_matrices => construct_matrices_sub
    end type matrices

    private :: allocator_rank_2_sub
    private :: construct_matrices_sub

    interface
        subroutine allocator_rank_2_sub ( array, rows, cols )
            use mPrecisionDefinitions, only : ip, rp
            real ( rp ), allocatable, intent ( out ) :: array ( : , : )
            integer ( ip ),           intent ( in )  :: rows, cols
        end subroutine allocator_rank_2_sub
    end interface

    contains
        subroutine construct_matrices_sub ( me, ints, mydof, measure )
            ...
                call me % allocator_rank_2 ( me % A, m, mydof )
            ...
        end subroutine construct_matrices_sub
end module mMatrices

子模块:

submodule ( mMatrices ) smAllocations
    contains    
        module subroutine allocator_rank_2_sub ( array, rows, cols )
            ...
        end subroutine allocator_rank_2_sub
end submodule smAllocations

通过make编译:

ftn -g -c -r s -o mod_precision_definitions.o mod_precision_definitions.f08
...
ftn -g -c -r s -o mod_matrices.o mod_matrices.f08
...
ftn -g -c -r s -o mod_sub_matrices_allocators.o mod_sub_matrices_allocators.f08
...
ftn -g -c -r s -o lsq.o lsq.f08
ftn -g -o lsq lsq.o --- mod_matrices.o --- mod_precision_definitions.o --- mod_sub_matrices_allocators.o ---
mod_matrices.o:(.data+0x1790): undefined reference to `allocator_rank_2_sub_'
make: *** [lsq] Error 1

`makefile'的最后一部分

# Main program depends on all modules
$(PRG_OBJ) : $(MOD_OBJS)

# resolve module interdependencies
...
mod_matrices.o                : mod_precision_definitions.o mod_parameters.o mod_intermediates.o
mod_sub_matrices_allocators.o : mod_matrices.o
...

机器:Cray XC30

编译器:Fortran 5.2.82

问题:需要修复什么?

包含@ IanH修正的更正代码片段:

interface
    MODULE subroutine allocator_rank_2_sub ( array, rows, cols )
        use mPrecisionDefinitions, only : ip, rp
        real ( rp ), allocatable, intent ( out ) :: array ( : , : )
        integer ( ip ),           intent ( in )  :: rows, cols
    end subroutine allocator_rank_2_sub
end interface

1 个答案:

答案 0 :(得分:2)

模块规范部分中allocator_rank_2_sub的接口块缺少MODULE前缀。这意味着它指定了外部过程,而不是预期的单独模块过程。然后链接器抱怨它找不到这样的外部程序。

将MODULE前缀添加到接口主体的子例程语句中。