目标:使用子程序load_things
加载一个类型为su2
的结构体库。
运行gfortran simple.f90
会产生以下结果:
Undefined symbols for architecture x86_64:
"_load_things_", referenced from:
_MAIN__ in cc7DuxGQ.o
(maybe you meant: ___myclass_MOD_load_things_sub)
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
主程序如下:
program simple
use myClass
implicit none
integer :: nThings
type ( su2 ) :: reflections
call load_things ( nThings, reflections )
end program simple
模块定义如下:
module myClass
implicit none
type :: su2
integer :: matrix_obj ( 1 : 2, 1 : 2 )
contains
private
procedure, nopass, public :: load_things => load_things_sub
end type su2
private load_things_sub
contains
subroutine load_things_sub ( nThings, U )
integer, intent ( out ) :: nThings
type ( su2 ), intent ( out ), allocatable :: U ( : )
nThings = 2
allocate ( U ( nThings ) )
U ( 1 ) % matrix_obj = reshape ( [ 0, 1, 1, 0 ], [ 2, 2 ] )
U ( 2 ) % matrix_obj = reshape ( [ 0, 1, 1, 0 ], [ 2, 2 ] )
end subroutine load_things_sub
end module myClass
以下网页尝试了但没有成功: Fortran中模块、子程序和函数的正确使用, Fortran 90 - 将值从主子程序传输到函数和其他子程序, Fortran: 在另一个模块的过程中调用模块中的函数, Fortran 90 如何在模块的子程序中调用一个函数?
type(su2) :: other_object; call other_object%load_things(nthings, reflections)
。但在没有其他信息的情况下,这似乎是无意义的复杂性。考虑到参考资料,我怀疑他们只想直接调用该过程。 - IanH