同一模块中的子例程未找到模块中的 Fortran 函数 [英] Fortran function in a module not found by subroutine in the same module
问题描述
我正在用Fortran90写一个模块,主要是在模块内部定义了一个函数,以及一个使用该函数的子程序.这是模块的摘录
I am writing a module in Fortran90, Mainly I defined a function inside the module, and a subroutine that uses the function. Here's an excerpt of the module
module Mesh_io
implicit none
private
contains
integer function findkey ( )
content of this function
end function
subroutine getNumber_Mesh ()
integer :: findkey
content of the routine
end subroutine getNumber_Mesh
end module
编译时我得到以下输出:
When compiling I get the following output:
objects/Main.o: In function `__mesh_io_MOD_getnumber_mesh':
Main.f90:(.text+0x9e): undefined reference to `findkey_'
如您所见,该函数包含在模块中,但由于某种原因编译器找不到它.
As you can see the function is contained in the module, but for some reason the compiler can not find it.
推荐答案
通过在子例程 getNumber_Mesh()
中声明 findkey,您将创建一个局部变量 findkey
隐藏功能.
With the declaration of findkey inside the subroutine getNumber_Mesh()
you are creating a local variable findkey
that hides the function.
对于模块,不需要声明(模块函数的)函数的返回值.简单地删除声明应该可以解决问题.
With modules, it is not required to declare the return value of functions (of module functions). Simply removing the declaration should do the trick.
这篇关于同一模块中的子例程未找到模块中的 Fortran 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!