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

问题描述 投票:2回答:1

我正在尝试创建一个具有子程序的模块,将另一个子程序的名称作为参数。这是一个主程序(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一切正常。然而,这不是我想要的。

你能帮我找一个创建模块的正确方法吗?我的代码出了什么问题?

fortran gfortran
1个回答
1
投票

你有与this other question相同的问题,所以请阅读那里的答案以获取更多细节。然而,这种情况有一些额外的复杂性值得考虑。

在子程序main的声明

   procedure(sub_interface) sub2,sub1

说有外部程序sub2sub1。模块sub1的模块子程序sub2action_mod不是这些外部程序。就链接问题而言,这就像声明character(255) strtok“隐藏”模块函数strtok一样。

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

但是您还有其他错误需要修复。模块子程序sub1sub2与抽象接口sub_interface没有相同的接口。你需要确保sub1sub2io的伪参数的intent属性匹配,sub_interface的那些。

© www.soinside.com 2019 - 2024. All rights reserved.