Fortran function in a module not found by subrouti

2019-01-20 14:34发布

问题:

This question already has an answer here:

  • Why is this a function declared inside the module and then used somewhere else in the same module not seen by the linker? 2 answers

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.

回答1:

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.