首页 > 解决方案 > Fortran主程序子例程可以链接模块变量但不能链接模块子例程?

问题描述

我有一个我制作的模块:

module my_sweet_module
complex, allocatable, volatile, save, private :: buffer_mem
integer mod_int1, mod_int2

contains

subroutine mod_sub1
include 'CRAPPY_OLD_IMPLICIT_PARAMETER_FILE'
<other code>
allocate(buffer_mem(CRAPPY_PARAMETER))
<more code>
end subroutine mod_sub1

end module my_sweet_module

当我在声明use之后的主文件中的这个模块时,无论我在什么范围内program,我都可以引用我在模块中声明的 - 事实上,由于名称冲突,我不得不重构我的代码以重命名任何实例.integersmod_int1

但是,任何时候我想mod_sub1在我的主文件中的子例程中使用,我必须use my_sweet_module在子例程的顶部有一个,否则当我尝试编译时会出现“未定义的引用”错误。

我的主文件示例:

program my_prog
include 'ANOTHER_CRAPPY_OLD_IMPLICIT_PARAMETER_FILE'
use my_sweet_module
<code>
end program my_prog



subroutine main_sub1(args)
include 'EVEN_MORE_CRAPPY_OLD_IMPLICIT_PARAMETER_FILE'
!use my_sweet_module !uncomment this to fix errors
integer sub_int1

sub_int1=mod_int1+1 !Compiler is happy; doesn't give any error, even if it "should"
call mod_sub1 !compiler is unhappy, says "undefined reference to 'mod_sub1_'"
end subroutine main_sub1

笔记:

我自己“解决”了我的问题,但我完全糊涂了。我要么不需要use到处都有,或者如果我不这样做,编译器不应该能够链接全局整数?两者都mod_int1应该mod_sub1超出范围,或者两者都不应该——错误不应该像这样混合和匹配。

标签: modulefortranlinker-errorsintelsubroutine

解决方案


推荐阅读