Fortran90模块中子程序无法找到同模块内函数的编译问题
findkey_ in Fortran 90 Module: Fix & Explanation Got it, let's break down why this linker error is happening even though your function and subroutine live in the same module, and how to fix it right away.
The root issue here is variable shadowing in your getNumber_Mesh subroutine:
- You added the line
integer :: findkeyinside the subroutine, which declares a local integer variable with the exact same name as your module'sfindkeyfunction. - In Fortran, local variables always take priority over module-level entities with matching names. This means the compiler thinks you're referring to this integer variable instead of the function. When linking, it can't find the
findkey_symbol because the subroutine isn't actually calling the module's function anymore.
The Fix
Just delete the integer :: findkey line from your getNumber_Mesh subroutine. Since both the function and subroutine are part of the same module, the subroutine has direct access to the findkey function—no need to re-declare it.
Here's the corrected module code:
module Mesh_io implicit none private contains integer function findkey () ! content of this function end function subroutine getNumber_Mesh () ! Removed the shadowing integer declaration here ! content of the routine ! Now you can call findkey() directly, e.g.: ! my_integer = findkey() end subroutine getNumber_Mesh end module
Quick Heads-Up
This kind of name clash is a common Fortran gotcha. Always double-check that your local variable names don't overlap with functions, subroutines, or other module-level entities—shadowing can lead to confusing errors like this one.
内容的提问来源于stack exchange,提问作者Andres Valdez

