Fortran link error: undefined reference using submodules
Fortran link error: undefined reference using submodules
错误信息:
mod_matrices.o:(.data+0x1790): undefined reference to `allocator_rank_2_sub_'
模块 mMatrices(在 mod_matrices.f08 中)调用子模块 smAllocations(在 mod_sub_matrices_allocators.f08 中)中的函数 allocator_rank_2_sub
。该代码在将模块 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
模块规范部分中 allocator_rank_2_sub 的接口块缺少 MODULE 前缀。这意味着它指定了一个外部过程,而不是预期的单独模块过程。然后链接器抱怨找不到这样的外部过程。
在接口体中的子程序语句中添加MODULE前缀
错误信息:
mod_matrices.o:(.data+0x1790): undefined reference to `allocator_rank_2_sub_'
模块 mMatrices(在 mod_matrices.f08 中)调用子模块 smAllocations(在 mod_sub_matrices_allocators.f08 中)中的函数 allocator_rank_2_sub
。该代码在将模块 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
模块规范部分中 allocator_rank_2_sub 的接口块缺少 MODULE 前缀。这意味着它指定了一个外部过程,而不是预期的单独模块过程。然后链接器抱怨找不到这样的外部过程。
在接口体中的子程序语句中添加MODULE前缀