对匹配抽象接口的模块子例程的未定义引用

Undefined reference to module subroutines matching an abstract interface

我正在尝试创建一个模块,其中的子例程将另一个子例程的名称作为参数。这是一个主程序(main.for):

    program testmod
    use action_mod

    call main

    end program testmod

这是我的模块 (action_mod.for) 的示例:

module action_mod

abstract interface        
subroutine sub_interface(aA, aB)
    integer aA, aB
    intent(in) aA
    intent(out) aB
end subroutine
end interface

contains

subroutine main
    procedure(sub_interface) sub2,sub1
    call action(sub1)
    call action(sub2)
end subroutine

subroutine action(sub)
    procedure(sub_interface) sub
    integer argA, argB
    argA = 10
    call sub(argA, argB)
    write(*,*) argA," > ",argB
end subroutine

subroutine sub1(i,o)
    integer i,o        
    o = 100        
    return
end subroutine sub1

subroutine sub2(i,o)
    integer i,o        
    o = 200        
    return
end subroutine sub2

end module action_mod

当我用

编译代码时
gfortran -o main action_mod.for main.for

我收到一个错误

/tmp/ccdSM11U.o: In function `__action_mod_MOD_main':
action_mod.for:(.text+0x1a2): undefined reference to `sub1_'
action_mod.for:(.text+0x1b1): undefined reference to `sub2_'
collect2: error: ld returned 1 exit status

但是当我将子例程 sub1(i,o)sub2(i,o) 放入 main.for 时一切正常。然而这不是我想要的。

你能帮我找到创建模块的正确方法吗?我的代码有什么问题?

您遇到的问题与 this other question 中的问题大致相同,因此请阅读那里的答案以获取更多详细信息。然而,这种情况有一些额外的复杂性值得考虑。

子程序中main语句

   procedure(sub_interface) sub2,sub1

表示有外部 过程sub2sub1。模块 action_mod 的模块子例程 sub1sub2 不是这些外部过程。就链接问题而言,这就像声明 character(255) strtok "hiding" 模块函数 strtok.

您应该从子例程中删除该语句。

但是您还有其他错误需要修复。模块子例程 sub1sub2 没有与抽象接口 sub_interface 相同的接口。您需要确保 sub1sub2io 的虚拟参数的意图属性与 sub_interface.[=28 的那些匹配=]