2

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.

Dan Sp.
  • 1,419
  • 1
  • 14
  • 21
Andres Valdez
  • 164
  • 17

1 Answers1

5

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.

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • Thanks for the answer @Alexander Vogt. The usage of modules is really new for me. This feedback really helped. – Andres Valdez May 17 '18 at 20:50
  • 1
    As the linker can't find a function `findkey_`, and the desired use of `findkey`, it's likely that other things in `getNumber_Mesh` mean that it is declaring an external function not a local variable. – francescalus May 18 '18 at 12:05