【问题标题】:Undefined reference to subroutine from module Fortran90 [duplicate]模块Fortran90对子程序的未定义引用[重复]
【发布时间】:2021-09-09 14:24:52
【问题描述】:

我对 fortran90 比较陌生,我需要一个项目。

我有三个脚本,两个模块和主程序,结构如下:

脚本 1:

program main
   use module1
   implicit none
   ..
   call sub_from_mod1
end program main

脚本 2:

module module_2
   implicit none
   contains
   ..
end module module_2

脚本 3:

module module_1
   use module_2
   implicit none
   contains 
   ...
   subroutine sub_from_mod1
   ...
end module module_1

在 CodeBlocks 中编译整个项目时,出现错误:

undefined reference to sub_from_mod1_

有谁知道是怎么回事?

【问题讨论】:

  • 欢迎您,请拨打tour 并阅读How to Askminimal reproducible example。请显示实际代码实际错误消息。此外,对所有 Fortran 问题使用标签 fortan
  • 您为编译和链接代码所做的工作也可能很有用 - 实际的命令。

标签: module fortran undefined-reference


【解决方案1】:

主程序的use语句有错别字:正确的应该是

use module_1 !not use module1

将主文件放在 main.f90 文件中,将模块放在 mod1.f90 和 mod2.f90 中,我可以编译代码:

gfortran -c mod2.f90
gfortran -c mod1.f90
gfortran -c main.f90
gfortran main.o mod1.o mod2.o

前三行的 -c 标志告诉编译器只编译而不链接代码。这是必要的,因为当程序链接到最后一行时,.mod 文件需要存在。

【讨论】:

  • 我怀疑这是真正的原因,如果模块不存在,错误消息会有所不同。我担心我们看到的代码被严重过度简化甚至残废。
猜你喜欢
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 2011-07-27
  • 2013-06-20
  • 2012-02-05
  • 2014-12-27
相关资源
最近更新 更多